Search code examples
htmlvbscriptconvertersdocx

vbscript to convert html files to docx


I have many html files in c:\temp\ which need to be converted in the same directory to .docx files.
I have Office 2013. So i guess it doesnt need any converter but instead only opening html in word in the background and saving it as a .docx.

I found a script which is converting doc to pdf: vbscript to convert word doc to pdf

Const wdExportAllDocument = 0
Const wdExportOptimizeForPrint = 0
Const wdExportDocumentContent = 0
Const wdExportFormatPDF = 17
Const wdExportCreateHeadingBookmarks = 1

if  Wscript.Arguments.Count > 0 Then
    ' Get the running instance of MS Word. If Word is not running, Create it
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err <> 0 Then
        Set objWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.GetFile(WScript.Arguments(0))
    Set objDoc = objWord.Documents.Open(WScript.Arguments(0),,TRUE)

    'Export to PDF using preferred settings
    pdf = objWord.ActiveDocument.ExportAsFixedFormat( _
        WScript.Arguments(1), _
        wdExportFormatPDF, False, wdExportOptimizeForPrint, _
        wdExportAllDocument,,, _
        wdExportDocumentContent, _
        False, True, _
        wdExportCreateHeadingBookmarks _
    )

    'Quit MS Word
    objWord.DisplayAlerts = False
    objWord.Quit(False)
    set objWord = nothing
    set objFSO = nothing
Else
    msgbox("You must select a file to convert")
End If

Changing the const wdExportFormatPDF to wdFormatDocumentDefault = 16 throws an error.

Any idea how to open and save as all files in the c:\temp directory?


Solution

  • The ExportAsFixedFormat method saves a document as PDF or XPS format..

    Read more about it here.

    For saving your file as docx, just use the SaveAs method. It is available for Office 2013 and later

    objWord.ActiveDocument.SaveAs ("C:\SomeDir\yourFileName.docx")