我想使用 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 做到这一点?
nb 我也在“ozgrid”论坛上问过这个问题。如果我找到解决方案,将关闭两个线程。