This is my code:
Set fso = CreateObject("Scripting.FileSystemObject")
strText = fso.OpenTextFile(strLocalFolderName & "\" & Oudste).ReadAll()
msgbox strText
But strText
contains rubbish after these lines.
How can that be?
This is my code:
Set fso = CreateObject("Scripting.FileSystemObject")
strText = fso.OpenTextFile(strLocalFolderName & "\" & Oudste).ReadAll()
msgbox strText
But strText
contains rubbish after these lines.
How can that be?
Darn! The boolean option within OpenTextFile examples is often left out!
fso.OpenTextFile(Path, ForReading, False, TriStateTrue)
Path is the path to the file. ForReading should be 1 for read only.
Then this False is the often omitted boolean (false means it's not written )
Only when the boolean is added correctly, you can pick a type of txt file.
In my case unicode so I pick -1 for the Tristate.
Tip: if you ever get weird results with textfiles, open in notepad, choose save as and then it will reveal what kind of text you actually have.
Your problem can be because a lot of thigs like the encode of target file, one of the most common encode us UTF-8 you can chage it with notepad++:
How do I convert an ANSI encoded file to UTF-8 with Notepad++?
I think you should put some validation code to find the real problem, I suggest this code:
ForReading=1 'Open a file for reading only. You can't write to this file.
ForWriting=2 'Open a file for writing.
ForAppending=8 'Open a file and write to the end of the file.
CreateIfNotExist=TRUE 'If you use FALSE you get error if not exist
set fso = CreateObject("Scripting.FileSystemObject")
if (fso.fileexists(".\test.txt")) then
set ts = fso.OpenTextFile(".\test.txt", ForReading, CreateIfNotExist)
if NOT ts.AtEndOfStream then
s = ts.ReadAll
msgbox s
else
msgbox "End of file"
end if
else
msgbox "File not found"
end if