vbscript to convert word doc to pdf

前端 未结 4 970
攒了一身酷
攒了一身酷 2021-01-16 14:12

I have written a short vbscript that opens a word document, edits a few bookmarks and saves to a new .doc file.

I now need to convert this to a pdf file, which is st

4条回答
  •  青春惊慌失措
    2021-01-16 14:45

    Rafael use CreateObject("Word.Application") for creating new MSWord process. In my system this code does not close word process correctly. but this code works correct

    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
    

    If this code save on word2pdf.vbs, it can called by this command at cmd:

    wscript word2pdf.vbs input.docx output.pdf
    

提交回复
热议问题