Append a header in a text file

aziz rasul

Active member
Local time
Today, 22:53
Joined
Jun 26, 2000
Messages
1,935
This is the problem.

I have a table that contains x records which I can export to a text file using the TransferText method in VBA. However before doing this I wish to place a single line of text at the beginning of the text file with certain information and THEN export the records from the table to the text file beginning at the second line.

Any ideas how to solve this problem in a simple manner using VBA.
 
This is a down and dirty way to do it. You're going to transfer the original file like you would normally.. Then you open a new file for output(FileName1) and the transferred file for input(FileName2). You put the header into FileName1 and then copy the contents of FileName2 into FileName1. At the end, you will delete the original file you created through the transfertext method and rename the new file the same name as the old file.

Function InsertHeader()
Dim FileName1 As String
Dim FileName2 As String
Dim strLine As String

FileName1 = "c:\NewText.txt"
FileName2 = "c:\TransferText.txt"

Dim FileNum As Integer
Dim FileNum2 As Integer

FileNum = FreeFile
FileNum2 = FreeFile + 1

Open FileName1 For Output As FileNum
Open FileName2 For Input As FileNum2
Print #FileNum, "This is the Header"

While Not EOF(FileNum2)
Input #FileNum2, strLine
Print #FileNum, strLine
Wend

Close #FileNum
Close #FileNum2

Kill FileName2
Name FileName1 As FileName2
End Function

Hope this helps....

Doug
 
Doug,

That worked. Many Thanks
 

Users who are viewing this thread

Back
Top Bottom