If you want to create a Text file separately based on your output, write it as a function to create text file and append it instead of using it in the elseif statement.
this will read the content of the text file then add "This is a new test" to the bottom line.
and then open the file for preview.
It won't recognize empty lines. I'm in a hurry now. I'll try to correct it later to be able to recognize the empty lines too later.
Public Sub CreateTextFile()
Dim i As Integer
Dim strLine As String
Dim strPreviousText As String
FilePath = "D:\temp.txt"
Open FilePath For Input As #1
i = 1
While EOF(1) = False
'read the next line of data in the text file
Line Input #1, strLine
'print the data in the current row
strPreviousText = strPreviousText & vbCrLf & strLine
'increment the row counter
i = i + 1
'Open the so you can write to it.
Open FilePath For Output As #1
'Write the Variable to the file
Print #1, strPreviousText & vbCrLf & "this is a new test"
'Close (and save) the file
f = "notepad.exe " & FilePath
'Open the file and view the variable
Shell "notepad.exe " & FilePath, vbNormalFocus
the reason why that does not work is because s u are telling it to create a new text file called test.txt every single time u run the macro
macro`s just overwrite data without asking
u need to add
Dim FilePath As String
Dim CheckFile As String
FilePath = "C:\Users\Anon\Desktop\test.txt"
CheckFile = ""
On Error Resume Next
CheckFile = Dir(FilePath)
On Error GoTo 0
If CheckFile = "" Then
Set oFile = fso.CreateTextFile("C:\Users\Anon\Desktop\test.txt")
MsgBox "File din't exist File Created ! "
Set oFile = fso.OpenTextFile("C:\Users\Anon\Desktop\test.txt")
To append to a file, you need to use OpenTextFile rather than CreateTextFile:
Set oFile = fso.OpenTextFile("C:\Users\Anon\Desktop\test.txt" ,ForAppending , True)
The last "True" means that if the file does not exist, then it will create it.
It's an integer. Actually, ForAppending = 8
But if you have Microsoft.ScriptingRuntime turned on in Tools / References, it will enable you to use ForAppending directly.
This works a treat, many thanks.