vbscript to convert word doc to pdf

前端 未结 4 926
攒了一身酷
攒了一身酷 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 15:06

    I once wrote a blog article on this matter. The conversion can be done as follows:

    Function DocToPdf( docInputFile, pdfOutputFile )
    
      Dim fileSystemObject
      Dim wordApplication
      Dim wordDocument
      Dim wordDocuments
      Dim baseFolder
    
      Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
      Set wordApplication = CreateObject("Word.Application")
      Set wordDocuments = wordApplication.Documents
    
      docInputFile = fileSystemObject.GetAbsolutePathName(docInputFile)
      baseFolder = fileSystemObject.GetParentFolderName(docInputFile)
    
      If Len(pdfOutputFile) = 0 Then
        pdfOutputFile = fileSystemObject.GetBaseName(docInputFile) + ".pdf"
      End If
    
      If Len(fileSystemObject.GetParentFolderName(pdfOutputFile)) = 0 Then
        pdfOutputFile = baseFolder + "\" + pdfOutputFile
      End If
    
      ' Disable any potential macros of the word document.
      wordApplication.WordBasic.DisableAutoMacros
    
      Set wordDocument = wordDocuments.Open(docInputFile)
    
      ' See http://msdn2.microsoft.com/en-us/library/bb221597.aspx
      wordDocument.SaveAs pdfOutputFile, wdFormatPDF
    
      wordDocument.Close WdDoNotSaveChanges
      wordApplication.Quit WdDoNotSaveChanges
    
      Set wordApplication = Nothing
      Set fileSystemObject = Nothing
    
    End Function
    

    It is important to close the files though. Note that you require Word 2007 with PDF plugin or Word 2010+ for doing this.

提交回复
热议问题