Unreadable characters in file with Print #File in VBA

P

pieros

I have a line in a text file in wich I want to replace some text.
I read a line of the file, check if it is the line to be modified. If
it is not than I go to the next line and so on till the end of the
file.
If it is the line to be modified than I replace the line part with the
correct text and want to write the corrected line back to the file with
Print #File.
So far looks good, but when I look in the corrected file in stead of
good characters I only see ascii squares.
(It is not the whole story but a short version of the actions)
Can someone help me with this please?
 
N

NickHK

You cannot just change some text in the middle of a file. (Well, if the
number of characters is the same, you could use Binary mode, but that does
not appear to be the case)
So you would need to read all unchanged text, write that to new file. Change
the incorrect line, write that to the file. Continue.

As long as your file are reasonably size, <5MB, you can read the whole file
into a string variable. Work on the variable with VBAs normal text functions
(Left, Instr, etc) and then write the variable to a new file. Kill the old
file if necessary. e.g.

Dim FileText As String
Dim FileNum As Integer

Const TestFilePath As String = "C:\Test.txt"

FileNum = FreeFile
Open TestFilePath For Input As #FileNum
FileText = Input$(LOF(FileNum), FileNum)
Close #FileNum

Debug.Print FileText
'Process the text in memeory
'And then write to a new file

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top