简体   繁体   中英

SaveAs2 output file in wrong encoding

I an new in VBA, I wrote this code to help to convert *.docx to *.txt . It gives me an output but in wrong encoding.

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

My test .docx file is wrote in Chinese and English, my output .txt file is encoding in ANSI , which should be UTF-8 in my desire. What did I do wrong?

Your code works fine
I use the text below in my docx file (english + chinese)

Here is my text in English
這是我的中文文本

the encoding UTF-8 is 65001 in Windows, so please could you check that you can save a .txt file with the UTF-8 encoding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM