This is what I use to send http pages and returns a string with the correct encoding
Public Function UTF8(ByVal http As Object) As String
Dim BinaryStream
Const adTypeBinary = 1
Const adTypeText = 2
Const adModeReadWrite = 3
Set BinaryStream = CreateObject("ADODB.Stream")
With BinaryStream
.Type = adTypeBinary
.Open
.Write http.responseBody
'Change stream type To binary
.Position = 0
.Type = adTypeText
'Specify charset For the source text
'.Charset = "iso-8859-1" 'unicode
.Charset = "utf-8" 'or utf-16
'Open the stream And get binary data from the object
UTF8 = .ReadText
End With
End Function
Where httpin this case is something like Set http = CreateObject("Microsoft.XMLHTTP"), but I'm sure you can adapt to your needs.
This works with strings and outputs a text file directly
Option Explicit
Sub test()
Dim filePath As String
Dim fileName As String
Dim charToEncode As String
Dim success As Boolean
filePath = "C:\Users\ooo\Desktop\"
fileName = "test.txt"
charToEncode = "Télécom"
success = ConvertToUTF8thenSaveToFile(charToEncode, filePath, fileName)
If success Then
MsgBox ("Success")
Else
MsgBox ("Failed")
End If
End Sub
Function ConvertToUTF8thenSaveToFile(ByVal charToEncode As String, _
ByVal filePath As String, ByVal fileName As String) As Boolean
Dim fsT As Object
Dim adodbStream As Object
On Error GoTo Err:
Set adodbStream = CreateObject("ADODB.Stream")
With adodbStream
.Type = 2 'Stream type
.Charset = "utf-8" 'or utf-16 etc
.Open
.WriteText charToEncode
.SaveToFile filePath & fileName, 2 'Save binary data To disk
End With
ConvertToUTF8thenSaveToFile = True
On Error GoTo 0
Exit Function
Err:
ConvertToUTF8thenSaveToFile = False
End Function
UPDATE: , , .
Option Explicit
Sub test()
Dim filePath As String
Dim fileName As String
Dim charToEncode As String
Dim encodingType As String
Dim success As Boolean
Dim rngArray() As Variant
filePath = "C:\Users\ooo\Desktop\"
fileName = "test.csv"
rngArray = Sheet1.Range("A1:E10000").Value
encodingType = "utf-8"
charToEncode = DelimitRange(rngArray)
success = ConvertToUTF8thenSaveToFile(charToEncode, filePath, fileName, encodingType)
If success Then
MsgBox ("Success")
Else
MsgBox ("Failed")
End If
End Sub
Function ConvertToUTF8thenSaveToFile(ByVal charToEncode As String, _
ByVal filePath As String, ByVal fileName As String, ByVal encodingCharSet As String) As Boolean
Dim fsT As Object
Dim adodbStream As Object
On Error GoTo Err:
Set adodbStream = CreateObject("ADODB.Stream")
With adodbStream
.Type = 2 'Stream type
.Charset = encodingCharSet 'or utf-16 etc
.Open
.WriteText charToEncode
.SaveToFile filePath & fileName, 2 'Save binary data To disk
End With
ConvertToUTF8thenSaveToFile = True
On Error GoTo 0
Exit Function
Err:
ConvertToUTF8thenSaveToFile = False
End Function
Function DelimitRange(ByVal XLArray As Variant) As String
Const delimiter As String = ","
Const lineFeed As String = vbCrLf
Const removeExisitingDelimiter As Boolean = True
Dim rowCount As Long
Dim colCount As Long
Dim tempString As String
For rowCount = LBound(XLArray, 1) To UBound(XLArray, 1)
For colCount = LBound(XLArray, 2) To UBound(XLArray, 2)
If removeExisitingDelimiter Then
tempString = tempString & Replace(XLArray(rowCount, colCount), delimiter, vbNullString)
Else
tempString = tempString & XLArray(rowCount, colCount)
End If
'Don't add delimiter to column end
If colCount < UBound(XLArray, 2) Then tempString = tempString & delimiter
Next colCount
'Add linefeed
If rowCount < UBound(XLArray, 1) Then tempString = tempString & lineFeed
Next rowCount
DelimitRange = tempString
End Function