2

Sorry about the wrong variable last time but it still acts the same. I must be doing something else wrong but I just can't see it. I'm having trouble reading files. It seems I am doing something wrong as I only get empty strings as answer (with either method). Files are .job but it doesn't work if I change them to .txt either. 'First is strange symbol (y with roof, p and b combined and L) and then gives empty strings and the amount isn't the same as number of lines in the document I've tried it several ways

MsgBox "Open: " & vbCR & FSO.GetAbsolutePathName(jFile) 'this is OK
                    Set objTextFile = objFSO.OpenTextFile(FSO.GetAbsolutePathName(jFile))
                    Do Until objTextFile.AtEndOfStream
                        strText = objTextFile.ReadLine
                        MsgBox strText
                    Loop
                    objTextFile.Close



Dim FSO, cFolder, jFolder, fName, fAnswer, dFolder, dFlag, objFSO, strText, splitText, objTextFile
Const ForReading=1
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
MsgBox "Open: " & vbCR & FSO.GetAbsolutePathName(jFile)'This works fine
                    Set objTextFile = objFSO.OpenTextFile _
                    (FSO.GetAbsolutePathName(jFile), ForReading)
                    Do Until objTextFile.AtEndOfStream
                        strText = objTextFile.ReadLine
                        MsgBox strText 'First is strange symbol (y with roof, p and b combined and L) and then gives empty strings and the amount isn't the same as number of lines in the document
                    Loop
                    objTextFile.Close
                    'Set objTextFile = objFSO.OpenTextFile(FSO.GetAbsolutePathName(jFile), ForReading)
                    'strText = objTextFile.ReadAll
                    'objTextFile.Close
                    'splitText= Split(strText, vbCrLf)
                    'For Each strLine in splitText
                        'MsgBox strLine
                    'Next
Bosskardo
  • 21
  • 3
  • 2
    You might be having issues with encoding, if it's not an ASCII text file you might want to use `ADODB.Stream` to convert it from one encoding to another. – user692942 Oct 19 '21 at 13:58
  • 1
    What you describe is consistent with the input file being encoded as Unicode (UTF-16). Try changing `ForReading` to `ForReading,,-1`. Yes, that's two commas and -1 means Unicode format. See the reference here: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method – LesFerch Oct 20 '21 at 00:57
  • Thank you. ForReading,,-1 corrected it – Bosskardo Oct 20 '21 at 14:05

0 Answers0