Consulting

Results 1 to 2 of 2

Thread: append text file

  1. #1

    append text file

    I have this code that adds to a text file. Is it possible to place the text at the beginning instead of the end?

    for example, in the file it has the following information:
    Comp I.D. , CompName , UserName

    Using the code below, it adds the txtName information at the end:
    Comp I.D. , CompName , UserName , txtName

    I would like it to place it at the beginning:
    txtName , Comp I.D. , CompName , UserName

    [VBA]Open MyPath & MyFile For Append As #1
    Print #1, vbTab; txtName;
    Close #1[/VBA]

    Can someone assist, thank you.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Rewrite the text file


    [VBA]Sub test()
    Dim FS, A
    Dim MyPath As String, MyFile As String, fil As String, X As String

    MyPath = "C:\AAA\"
    MyFile = "New.txt"

    fil = MyPath & MyFile

    Set FS = CreateObject("Scripting.FileSystemObject")
    Set A = FS.OpenTextFile(fil, 1)
    X = A.ReadAll
    A.Close
    Kill fil

    Set A = FS.CreateTextFile(fil)
    A.WriteLine ("This is a test.")
    A.Write (X)
    A.Close


    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •