How to read unicode and write out to text file, with macro?

ericblair

New Member
Joined
Sep 18, 2009
Messages
5
I have some worksheets that contains chinese characters (unicode I presume.) I am looping through sheets and writing the stuff out to a text file, however, when I open the text file the chinese chars are converted to "????"

This is the code I use to read in the chars from excel:

Code:
Do While CellColCount <= ColCount
 StringStore = StringStore + " '" + CStr(Current.Cells(Row, CellColCount + 1)) + "'"
 If CellColCount <> ColCount Then
 StringStore = StringStore + ", "
 End If
 CellColCount = CellColCount + 1
 Loop

And here I write it out:

Code:
Print #fHandle, StringStore

My question: How do I read the chinese chars and qrite them out to the text file also in VBA?

Thanks!!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi

Maybe this can get you started.

I compose a string with the values in A1:A3 and write it into a text file that supports Unicode.

1 - in the vbeditor set the reference Tools->References->Microsoft Scripting Runtime

2 - Write some values in A1:A3, using some of your chinese characters, and try:

Code:
Sub Test()
Dim s As String
Dim fs As FileSystemObject
Dim txtf As TextStream

' write the text of A1:A3 separated with commas into s
s = Join(Application.Transpose(Range("A1:A3")), ",")

' write the string into a text file
Set fs = New FileSystemObject
Set txtf = fs.CreateTextFile(Filename:="c:\tmp\excel\test.txt", Overwrite:=True, Unicode:=True)
txtf.WriteLine s
txtf.Close
End Sub

Remark: don't forget to change the pathname of the output file, as you may not have the folder I use.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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