You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

需求:将C:\temp\下HTML文件批量转DOCX(Office2013环境)

Batch Convert HTML to DOCX Using VBScript (Office 2013)

Since you’re using Office 2013, you’re totally right—Word can natively open HTML files and save them as DOCX without any extra tools. Here’s a modified VBScript based on your DOC-to-PDF example that handles batch conversion for all HTML files in C:\temp\:

' Constant for Word's DOCX file format
Const wdFormatXMLDocument = 12

' Create Word application (runs in the background)
Set objWord = CreateObject("Word.Application")
objWord.Visible = False ' Keep Word hidden during conversion
objWord.DisplayAlerts = 0 ' Suppress pop-up prompts/alerts

' Target folder containing HTML files
strFolder = "C:\temp\"

' Set up file system object to scan the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

' Loop through every file in the folder
For Each objFile In objFolder.Files
    ' Check for .html or .htm extensions (case-insensitive)
    strFileExt = LCase(objFSO.GetExtensionName(objFile.Name))
    If strFileExt = "html" Or strFileExt = "htm" Then
        strHTMLPath = objFile.Path
        ' Generate output DOCX filename (replace extension)
        strDOCXPath = objFSO.BuildPath(strFolder, objFSO.GetBaseName(objFile.Name) & ".docx")
        
        ' Open the HTML file in Word
        Set objDoc = objWord.Documents.Open(strHTMLPath)
        
        ' Save the document as DOCX
        objDoc.SaveAs strDOCXPath, wdFormatXMLDocument
        
        ' Close the document to free up resources
        objDoc.Close
        
        Set objDoc = Nothing
    End If
Next

' Clean up and quit Word
objWord.Quit
Set objWord = Nothing
Set objFSO = Nothing
Set objFolder = Nothing

WScript.Echo "Batch conversion finished successfully!"

How to Use This Script:

  1. Open Notepad, paste the code above, and save the file with a .vbs extension (like HTMLtoDOCX.vbs).
  2. Double-click the saved script—Word will run silently in the background while converting files.

Quick Notes:

  • The script will overwrite any existing DOCX files with the same name in the folder (add an If objFSO.FileExists(strDOCXPath) Then check if you want to skip duplicates).
  • If you run into errors, make sure Office 2013 is fully installed and registered on your system, or try running the script with admin permissions.
  • Corrupted or malformed HTML files might cause conversion issues—you can add basic error handling with On Error Resume Next if needed.

内容的提问来源于stack exchange,提问作者Lucas

火山引擎 最新活动