Import text file as UTF-8

tabrizbalasi

New Member
Joined
Sep 14, 2014
Messages
2
I am using the following vba code to import all txt files in the active directory into excel.

Code:
Sub ImportTXTfiles()
Dim Path As String

Path = Application.ActiveWorkbook.Path & "\"
Filename = Dir(Path & "*.txt")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets

Sheet.Copy after:=ThisWorkbook.Sheets("Sheet1")

Next Sheet
Workbooks(Filename).Close
Filename = Dir()

Loop

End Sub

However my txt files are in UTF-8 character encoding. How can I import them using the above code but in UTF-8 code?

I tried to use this:
Code:
With CreateObject("ADODB.Stream")
      .Charset = "utf-8" 'Specify charset For the source text data.
End With

but without success. Your help is appreciated.

Could you please help me to modify this formula to import txt files in utf-8? thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi
Welcome to the board

This is an example. I read the first 10 characters of a file with UTF-8 format.

In the <acronym title="visual basic for applications">vba</acronym> editor set the reference to "Microsoft Active Data Objects 6.1 Library"

Code:
Sub TestR_utf_8()
Dim st As ADODB.Stream
Dim sPathname As String, sText As String

sPathname = "c:\tmp\test_utf-8.txt"

' create a stream object
Set st = New ADODB.Stream

' set properties
st.Charset = "utf-8"
st.Type = adTypeText

' open the stream object and load the text
st.Open
st.LoadFromFile (sPathname)

' read 10 characters
sText = st.ReadText(10)

' display the characters read
MsgBox sText

st.Close
Set st = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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