Excel VBA - export to UTF-8
Posted
by
Tom
on Stack Overflow
See other posts from Stack Overflow
or by Tom
Published on 2012-09-10T13:40:53Z
Indexed on
2012/09/11
9:38 UTC
Read the original article
Hit count: 322
The macro I created works fine, I just need to sort out the saving business. Now I get a popup asking me where to save it, but I would like it to save it under a default name and path AND encoded in UTF-8.
This is my full code I use, the bottom part saves the document I presume.
Public Sub ExportToTextFile(FName As String, Sep As String, SelectionOnly As Boolean, AppendData As Boolean)
Dim WholeLine As String
Dim fnum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Dim teller As Integer
'Teller aangemaakt ter controle voor het aantal velden
'teller = 1
Application.ScreenUpdating = False
On Error GoTo EndMacro:
fnum = FreeFile
If SelectionOnly = True Then
With Selection
StartRow = .Cells(1).Row
StartCol = .Cells(26).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(26).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(26).Column
End With
End If
If AppendData = True Then
Open FName For Append Access Write As #fnum
Else
Open FName For Output Access Write As #fnum
End If
For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = ""
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
Print #fnum, WholeLine, ""
'Print #fnum, teller, WholeLine, ""
'teller = teller + 1
Next RowNdx
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #fnum
End Sub
Sub Dump4Mini()
Dim FileName As Variant
Dim Sep As String
FileName = Application.GetSaveAsFilename(InitialFileName:=Blank, filefilter:="Text (*.txt),*.txt")
If FileName = False Then
Exit Sub
End If
Sep = "|"
If Sep = vbNullString Then
Exit Sub
End If
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ExportToTextFile FName:=CStr(FileName), Sep:=CStr(Sep), SelectionOnly:=False, AppendData:=False
End Sub
© Stack Overflow or respective owner