Problem with .WriteLine and .OpenTextFile(..., 8, ...)

spaghetti

New Member
Joined
Jul 17, 2013
Messages
5
I want to generate a log while my VBA program runs. Firstly, I initialize the log file and add a title. This works. Then, I want to add info (AddinfoLog). When I open the textfile in MS Wordpad, I see Chinese character intead of "test" on the last row. When I change the 8 to a 2 to write (and supersede the original file) as a test, I see "test" in the file, which is correct.
In each case, when opening the txt file in the MULTI Editor, i always see "test", and never the Chinese characters.
How can I solve this problem?

Code:
Code:
Public fso As FileSystemObject
Public FSOFile As TextStream
Public LogPath As String

Sub InitializeLog()[INDENT]Set fso = New FileSystemObject
LogPath = mainfolder & "\Log.txt"

Set FSOFile = fso.CreateTextFile(LogPath, 1, True)

FSOFile.WriteLine "Title"
FSOFile.WriteLine "Generation started on " & Date & " at " & Time
FSOFile.Close[/INDENT]
End Sub

Sub AddinfoLog()
[INDENT]Set fso = New FileSystemObject
LogPath = mainfolder & "\Log.txt"

Set FSOFile = fso.OpenTextFile(LogPath, 8, True) '2 for write, 8 for append 'else error bad file mode

FSOFile.WriteLine "test"

FSOFile.Close[/INDENT]
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
1. When I change LogPath to mainfolder & "\Log.rtf" and I open the rtf file with MS Word, I get a file conversion box ('Select the encoding that makes your document readable.'). The default choice is 'Other encoding: Unicode', and then I get the version with the Chinese characters. If I change the setting to 'Windows (Default)', it shows the same content as the MULTI Editor did, without the Chinese characters.
2. Regardless of the Chinese characters problem, spaces are added between each character. How can I solve this?
It seems to be an encoding problem.
 
Upvote 0
If I don't close the file and reopen it in the following function, everything works fine. What should I do to let it work correctly with closing and reopening?

Code:
Public fso As FileSystemObject
Public FSOFile As TextStream
Public LogPath As String
Sub InitializeLog()[INDENT]Set fso = New FileSystemObject
LogPath = mainfolder & "\Log.txt"

Set FSOFile = fso.CreateTextFile(LogPath, 1, True)
FSOFile.WriteLine "Title"FSOFile.WriteLine "Generation started on " & Date & " at " & Time
'FSOFile.Close '<-- inactive[/INDENT]
End Sub
Sub AddinfoLog()[INDENT]Set fso = New FileSystemObject
LogPath = mainfolder & "\Log.txt"
'Set FSOFile = fso.OpenTextFile(LogPath, 8, True) '2 for write, 8 for append 'else error bad file mode '<-- inactive
FSOFile.WriteLine "test"
FSOFile.Close[/INDENT]
[FONT=monospace]End Sub[/FONT]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top