This smells like VBA, not true VB. You have posted in the wrong forum.
It is also being misused or you (a.) would not have to define those constants, and (b.) through IntelliSense you probably would have discovered your error.
If you had set a reference to Microsoft Scripting Runtime you'd be in less of a mess. As it stands this looks like something largely copy/pasted from a musty old VBScript example.
The upshot is that your CreateTextFile() call is explicitly creating a Unicode file but your OpenTextFile() call omits its Format argument and implicitly opens as ASCII (not even ANSI). Your solution lies in reading the fine manual: OpenTextFile Method.
There is a lot of other messy stuff there, all you need is:
Code:Option Explicit 'Requires a reference to: Microsoft Scripting Runtime. Function LogMessage(ByRef Message As String) Dim FSO As Object Dim LogFileName As String Dim File As Object Set FSO = New Scripting.FileSystemObject #If VBA Then LogFileName = CurrentProject.Path & "\MyLog.txt" #Else LogFileName = App.Path & "\MyLog.txt" #End If If FSO.FileExists(LogFileName) Then Set File = FSO.OpenTextFile(LogFileName, ForAppending, False, TristateTrue) Else Set File = FSO.CreateTextFile(LogFileName, True, True) End If File.WriteLine CStr(Now) & ": " & Message File.Close End Function




Reply With Quote
