I need to insert a Header row to existing text file (VBA Code).
Here is my code;
Sub header()
'the final string to print in the text file
Dim strData As String
'each line in the original text file
Dim strLine As String
Dim time_date As String
strData = ""
time_date = Format(Date, "yyyymmdd")
'open the original text file to read the lines
Open "C:\Temp\Test.txt" For Input As #1
'continue until the end of the file
While EOF(1) = False
'read the current line of text
Line Input #1, strLine
'add the current line to strData
strData = strData + strLine & vbCrLf
Wend
'add the new line
strData = "header row" & " " + time_date + " " + strData
Close #1
'reopen the file for output
Open "c:\Temp\Test.txt" For Output As #1
Print #1, strData
Close #1
End Sub
It is appending Header row to FIRST row data instead Inserting a new row to top.....
For Example:
header row 20140812 HELLO THIS IS MY TEST
I need;
header row 20140812
HELLO THIS IS MY TEST ...
Thanks for your help!
Here is my code;
Sub header()
'the final string to print in the text file
Dim strData As String
'each line in the original text file
Dim strLine As String
Dim time_date As String
strData = ""
time_date = Format(Date, "yyyymmdd")
'open the original text file to read the lines
Open "C:\Temp\Test.txt" For Input As #1
'continue until the end of the file
While EOF(1) = False
'read the current line of text
Line Input #1, strLine
'add the current line to strData
strData = strData + strLine & vbCrLf
Wend
'add the new line
strData = "header row" & " " + time_date + " " + strData
Close #1
'reopen the file for output
Open "c:\Temp\Test.txt" For Output As #1
Print #1, strData
Close #1
End Sub
It is appending Header row to FIRST row data instead Inserting a new row to top.....
For Example:
header row 20140812 HELLO THIS IS MY TEST
I need;
header row 20140812
HELLO THIS IS MY TEST ...
Thanks for your help!