?
Solved

Export with VB/Macro from excel, unicode characters to text files

Posted on 2010-11-22
1
Medium Priority
?
1,103 Views
Last Modified: 2012-05-10
Hi all,

I need to export the rows from the attached excel file in to multiple txt files. Each row will be exported to a different txt file with filename the last columns data.

I have written a VB code but it doesn't work with unicode data. I need to export Greek text in the file. Can anyone help by writing a new code or by tweaking mine?
Book1.xls

Sub TextFile()
Dim fs As Object, a As Object, i As Long, s As String, lngLastRow As Long
Set fs = CreateObject("Scripting.FileSystemObject")
lngLastRow = Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To lngLastRow Step 1
  Set a = fs.CreateTextFile("C:\Temp\" & Range("G" & i).Value & ".txt", True)

  s = s & Cells(i, 1) & vbCrLf & Cells(i, 2) & vbCrLf & Cells(i, 3) & vbCrLf & Cells(i, 4) & vbCrLf & Cells(i, 5) & vbCrLf & Cells(i, 6) & vbCrLf & Cells(i, 7) & vbCrLf & Cells(i, 8) & vbCrLf & Cells(i, 9)


  a.WriteLine s
  s = ""
  a.Close
Next i
End Sub

Open in new window

0
Comment
Question by:gkarydis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
canali earned 500 total points
ID: 34192850

object.CreateTextFile(filename[, overwrite[, unicode]])
You need to tell that your file is unicode if the third param is omited the file is ASCII.

http://msdn.microsoft.com/en-us/library/5t9b5c0c(VS.85).aspx

Sub TextFile()
Const Unicode = -1
...
...
Set a = fs.CreateTextFile("C:\Temp\" & Range("G" & i).Value & ".txt", True, Unicode)
...
...
end sub
Sub TextFile()
Const Unicode = -1


Dim fs As Object, a As Object, i As Long, s As String, lngLastRow As Long
Set fs = CreateObject("Scripting.FileSystemObject")
lngLastRow = Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To lngLastRow Step 1
  Set a = fs.CreateTextFile("C:\Temp\" & Range("G" & i).Value & ".txt", True, Unicode)

  s = s & Cells(i, 1) & vbCrLf & Cells(i, 2) & vbCrLf & Cells(i, 3) & vbCrLf & Cells(i, 4) & vbCrLf & Cells(i, 5) & vbCrLf & Cells(i, 6) & vbCrLf & Cells(i, 7) & vbCrLf & Cells(i, 8) & vbCrLf & Cells(i, 9)


  a.WriteLine s
  s = ""
  a.Close
Next i
End Sub

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question