VBA Read accented characters for Input

bettlejus

New Member
Joined
Apr 4, 2017
Messages
32
Hi all,

I have the following code which I use to read TXT file name/file content to excel file from a location.

The problem is that I have some accented characters in TXT files and they end up in excel as "Ä" - "È™" - ""

How can I modify the code to specify the encoding or solve this problem?

Code:
Sub TxtCopy()
    
    Dim sPath As String
    Dim iRow As Long
    Dim strString  As String
    Dim strString1  As String
    Dim strString2  As String
    
    Dim fso As FileSystemObject
    Dim xFile As File
    Dim xFolder As Folder
    Dim Location As Variant
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Location = InputBox("Introduceti calea fisierelor TXT")
    
    Set xFolder = fso.GetFolder(Location & "\")
    
    iRow = 1 ' Row to start inserting data
    
    For Each xFile In xFolder.Files
            
        If InStr(1, xFile.Name, ".txt") <> 0 Then
        
            Dim lFile As Long
            Dim szLine As String
            
            lFile = FreeFile()
            
            Open xFile.Path For Input As lFile
            
            strString = ""
            While Not EOF(lFile)
            
                Line Input #lFile, szLine
            
                ' Concatenete lines from text file
                strString = strString & szLine & vbCrLf
                strString1 = xFile.Name
                strString2 = xFolder.Name
                                
            Wend
            
            ' Add to cell
            Cells(iRow, 2).Value = strString
            Cells(iRow, 1).Value = Left(strString1, Len(strString1) - 4)
            Cells(iRow, 3).Value = strString2
            
            
            iRow = iRow + 1
            
            
            ' Close the file
            Close lFile
            
            Application.ScreenUpdating = True
                        
        End If
        
    Next ' End of LOOP
    
    MsgBox "Completed!"
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I tried to read the file with ADODB>Stream in UTF-8 enconding:


Code:
Dim objStream, strData
Set objStream = CreateObject("ADODB.Stream")
objStream.Charset = "utf-8=bom"
objStream.Open
objStream.LoadFromFile ("C:\ACC_VBA\descriere.html")
strData = objStream.ReadText()
MsgBox strData


But till my characters are replaced with question marks ??

I have romania accented characters in LATIN10 format or ISO 8859-16....I don;t see any wah to define the 8859 charset in ADODB.Stream.

Please help!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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