Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Write to File with Header (XP)

    How would one change the code below to incorporate a field header line into the text document? In essense, I would like to 'write' oHeader to the text file and then 'append' oText2File.

    I eventually will be looping and appending different lines of text via the 'oText2File' variable.

    Example of code:
    Sub Write_To_File
    Dim oHeader As String
    File = "C:TempTestFile.txt"

    oHeader = "BUnit,Account,Descr,Amount"
    oText2File ' "Blah, Blah, Blah"

    Close #1
    Open File For Output As #1
    Write #1, oText2File

    Close #1
    End Sub


    Thanks,
    John

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Write to File with Header (XP)

    Insert the line

    Write #1, oHeader

    immediately below the line that opens the text file.

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write to File with Header (XP)

    Hans,

    That will work if I want to have multiple header lines. The code would initially put in a header line, text line and then loop through appending another header and text line and so forth.

    Regards,
    John

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Write to File with Header (XP)

    You can start the loop after writing the header line, can't you?

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write to File with Header (XP)

    Hans,

    It did not occur to me. I was off on a different tangent.

    When I write 'oHeader' to the text file, the first and last characters are quote marks. How would I omit them? As you can see by the Dim statement, I have it as a string. I also tried just the "Dim oHeader" but end up with the same results.

    Thanks,
    John

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Write to File with Header (XP)

    Write always "formats" the output. Use Print instead of Write if you don't want quotes around text strings.

  7. #7
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write to File with Header (XP)

    Thank you

Posting Permissions

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