+ Reply to Thread
Results 1 to 6 of 6

appending line to text file

  1. #1
    Registered User
    Join Date
    06-24-2006
    Posts
    13

    appending line to text file

    Hi,

    Hope someone can help with this one.

    I've written a macro that creates a text file with 3 columns of data.

    I now need to add a line at the beginning of the text file. I can't add it before saving as text for example because then the text file contains tabs in A2 and A3 and this is a problem in this application.

    So I need to find some way to add a line of text to the beginning of a text file in an excel macro.

    how would I go about writing directly to a text file without needing to open the file in excel, adding text to a cell and closing again??

    Any ideas?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello lif,

    Provided your file has a .txt extension, you can open and modify it using NotePad.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    06-24-2006
    Posts
    13
    Hi Leith,

    Sorry I wasn't clearer. I need to create a txt file from an excel worksheet. I've written the macro that parses the data I need and saves the results as .txt . However, I also need this .txt file to have the following phrase "mesh autocreate" as the first line.

    If I add the phrase "mesh autocreate" to the excel sheet in cell a1 in the macro then when it is saved as .txt there are 2 tab characters (corresponding to A2 and A3 since there are 3 columns of data)

    I can open the file in notepad and remove the tabs manually - but the next part of the macro I'm writing uses shell() to start a different bit of software that uses this text file so it needs to be done within the macro.

    So I need to find a way to add that text to the beginning of the file without the tabs.

    I believe there is some way to manipulate the actual text file with visual basic to get this done. But have no idea how it's done.

  4. #4
    Hemant_india
    Guest

    Re: appending line to text file

    hi lif
    if you are using open stattement
    it should be easy for u
    read help topic for open statement
    --
    hemu


    "Leith Ross" wrote:

    >
    > Hello lif,
    >
    > Provided your file has a .txt extension, you can open and modify it
    > using NotePad.
    >
    > Sincerely,
    > Leith Ross
    >
    >
    > --
    > Leith Ross
    > ------------------------------------------------------------------------
    > Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
    > View this thread: http://www.excelforum.com/showthread...hreadid=563543
    >
    >


  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello lif,

    This code adds the line "mesh autocreate" to the file designated by the variable OrigDoc. A new file, NewDoc, is opened and the line is added. The contents of OrigDoc are then read and copied into it. Lastly, the OrigDoc is deleted and NewDoc is renamed as OrigDoc. Be sure to change the DocName and DocPath variables to match your own.

    This could also be done using API calls. However, I am not sure how you would envoke them outside of VBA.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  6. #6
    Registered User
    Join Date
    06-24-2006
    Posts
    13
    Hi Leith,

    Here's a belated thanks! I solved it in a very similar way. Here's my code for posterity

    Dim FinalFile As Integer
    Dim PreFile As Integer
    Dim PreName(1 To 25) As String
    Dim FinalName(1 To 25) As String
    Dim fso As Object
    Dim Temp As String


    FinalName(numberofwafermaps) = WafermapPath & ERsheetname(I) & ".txt"
    PreName(numberofwafermaps) = WafermapPath & ERsheetname(I) & "-pre.txt"
    FinalFile = FreeFile()


    Set fso = CreateObject("Scripting.FileSystemObject")

    If (fso.FileExists(FinalName(numberofwafermaps))) Then
    Kill FinalName(numberofwafermaps)
    End If

    Close #FinalFile
    Open FinalName(numberofwafermaps) For Append As FinalFile
    Print #FinalFile, "Mesh AutoCreate"


    PreFile = FreeFile()

    Open PreName(numberofwafermaps) For Input As PreFile
    '
    Do While Not EOF(PreFile)
    Line Input #PreFile, Temp
    Print #FinalFile, Temp
    Loop

    'CloseFiles:
    '
    ' ' Close the destination file and the source file.
    Close #PreFile
    Close #FinalFile

    If (fso.FileExists(PreName(numberofwafermaps))) Then
    Kill PreName(numberofwafermaps)
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1