编写 MS Word 2010 脚本以打开 HTM 文件并另存为 DOCX 文件

Zac*_*ach 4 microsoft-word microsoft-word-2010

我有一堆 .htm 文件,我希望它们是 .docx 文件。如何编写 Word 脚本以打开它们并将它们另存为 .docx 文件?

我可以手动启动该过程。我不需要等待更多文件的东西,只需要可以按需运行并处理整个文件文件夹的东西。

nix*_*xda 5

Sub ConvertHTMtoDOCX()

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        .Show
        strPath = .SelectedItems.Item(1) + "\"
    End With

    strFilename = Dir(strPath & "*.htm")

    While Len(strFilename) <> 0
        Set myDoc = Documents.Open(strPath & strFilename)
        strNewName = strPath & strFilename & ".docx"
        myDoc.SaveAs FileName:=strNewName, FileFormat:=wdFormatDocumentDefault
        myDoc.Close SaveChanges:=wdDoNotSaveChanges
        strFilename = Dir()
    Wend

End Sub
Run Code Online (Sandbox Code Playgroud)

在我的桌面、Windows 7 x64 和 Office 2013 上测试了 2 个 .HTM 文件