Link to home
Start Free TrialLog in
Avatar of mcelrogr
mcelrogr

asked on

TextStream Not Reading .TXT Text Correctly

When I open a .TXT file in Notepad it reads correctly.

Using VBA, when I read the .TXT file using a TextStream I get characters like: Â+áÏ"ûý

I have tried changing the TriState values and I continue to get these strange characters.

Any suggestions?  Thanks for the help!
Avatar of Norie
Norie

Can you post the code you are using to open the TextStream in VBA and/or attach a small sample file?
You're reading the file using the wrong encoding. So, when I need to guess, you're reading a non-ASCII file as ASCII file using the default methods.

Test using ADODB.Stream (Access):

Option Compare Database
Option Explicit

Private Sub Command2_Click()
  
  Dim content As String
  
  content = ReadUnicodeFile("C:\Temp\Test.txt")
  Debug.Print content
  Text0.Value = content
  
End Sub

Public Function ReadUnicodeFile(AFileName As String) As String

  Dim fileStream As ADODB.stream
  Set fileStream = New ADODB.stream

  fileStream.type = StreamTypeEnum.adTypeText
  fileStream.Charset = "utf-8"
  fileStream.Open
  fileStream.LoadFromFile AFileName
  ReadUnicodeFile = fileStream.ReadText(StreamReadEnum.adReadAll)
  fileStream.Close

  Set fileStream = Nothing

End Function

Open in new window

Avatar of mcelrogr

ASKER

I should have been more specific... A .PDF file is being saved as a .TXT file.  When I open the .TXT file, all of the characters are appearing correctly.  When I use the code provided by ste5an or the code below, I get all of those weird characters.  I tried a few different charsets from ste5an's code without success.

Public Function OutlookSession_ItemAttachment(ByVal Item As Object) As String

    Dim DocumentObject As New FileSystemObject, SequenceObject As TextStream
    Set SequenceObject = DocumentObject.OpenTextFile(Environ("USERPROFILE") & "\Documen" & _
        "ts\Outlook Files\" & CStr(Item.EntryID) & ".txt")
            
    OutlookSession_ItemAttachment = SequenceObject.ReadAll
        
    SequenceObject.Close
    
End Function

Open in new window

To my knowledge, PDF is a binary format, and it isn't free.

You just can't change the file extention to .txt and expect to read it as you want. Use an appropriate library for that.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial