我是VBA中的一个新手,我编写了这段代码以帮助将*.docx
转换为*.txt
。它给了我一个输出,但编码错误。
Sub FileConverter()
On Error Resume Next
Dim sEveryFile As String, sSourcePath As String, sNewSavePath As String, InFileSuffix As String, OutFileSuffix As String
Dim CurDoc As Object, WordApp As Object
Set WordApp = CreateObject("Word.Application")
sSourcePath = "D:\test"
sEveryFile = Dir(sSourcePath & "\*.docx")
Do While sEveryFile <> ""
Set CurDoc = WordApp.Documents.Open(sSourcePath & "\" & sEveryFile, , , , , , , , , , , msoFalse)
sNewSavePath = VBA.Strings.Replace(sSourcePath & "\" & sEveryFile, ".docx", ".txt")
CurDoc.SaveAs2 sNewSavePath, Encoding:=65001, FileFormat:=wdFormatText
CurDoc.Close SaveChanges:=False
sEveryFile = Dir
Loop
MsgBox "Done"
Set CurDoc = Nothing
End Sub
我的测试.docx
文件是用中文和英文写的,输出的.txt
文件是用ANSI
编码的,这应该是我想要的UTF-8
。我做错什么了?
发布于 2020-09-25 06:18:07
您的代码运行良好。
我在docx文件中使用以下文本(英文+中文)
Here is my text in English
這是我的中文文本
在Windows中编码UTF-8是65001,所以请您检查是否可以用UTF-8编码保存.txt文件。
https://stackoverflow.com/questions/64062513
复制