我想使用VBA导出一个我已经创建的UTF-8 CSV文件。从搜索论坛中,我找到了以下代码,可以将文件转换为UTF-8(从这个帖子):
Sub SaveAsUTF8()
Dim fsT, tFileToOpen, tFileToSave As String
tFileToOpen = InputBox("Enter the name and location of the file to convert" & vbCrLf & "With full path and filename ie. C:\MyFolder\ConvertMe.Txt")
tFileToSave = InputBox("Enter the name and location of the file to save" & vbCrLf & "With full path and filename ie. C:\MyFolder\SavedAsUTF8.Txt")
tFileToOpenPath = tFileToOpen
tFileToSavePath = tFileToSave
Set fsT = CreateObject("ADODB.Stream"): 'Create Stream object
fsT.Type = 2: 'Specify stream type – we want To save text/string data.
fsT.Charset = "utf-8": 'Specify charset For the source text data.
fsT.Open: 'Open the stream
fsT.LoadFromFile tFileToOpenPath: 'And write the file to the object stream
fsT.SaveToFile tFileToSavePath, 2: 'Save the data to the named path
End Sub
然而,这段代码只能将非UTF-8文件转换为UTF-8。如果我将我的文件保存为非UTF-8,然后再将其转换为UTF-8,那么它已经丢失了其中包含的所有特殊字符,从而使该过程毫无意义!
我想要做的是将一个打开的文件保存为UTF-8(CSV)。有没有办法在VBA中实现这一点?
注:我也在'ozgrid'论坛上提出了这个问题。如果找到解决方案,将同时关闭两个线程。