Dave
Dave

Reputation: 387

vbscript to convert word doc to pdf

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 straightforward enough to do with something like cutePDF (by sending it to a virtual printer), but I would like to automate that step.

Could anyone help with any ideas on the vbscript necessary for that process, either automating the print step, or another method.

Many thanks

Dave

Upvotes: 2

Views: 24974

Answers (5)

Kirill Frolov
Kirill Frolov

Reputation: 1

Use convert.vbs fullFilePath/fileName.docx fullFilePath/filename.pdf.

Dim WA, WD, Args
Set Args = WScript.Arguments
Set WA = CreateObject("Word.Application")
Set WD = WA.Documents.Open(Args(0))
WD.SaveAs Args(1), 17
WD.Close
Set WD = Nothing
WA.Quit
Set WA = Nothing

Upvotes: 0

IT man
IT man

Reputation: 603

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

Upvotes: 0

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44042

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.

Upvotes: 3

Simon
Simon

Reputation: 797

If you're using Word 2003, you're going to need some sort of external library/file to do the conversion.

Would something like this help: http://www.verypdf.com/pdfcamp/word-to-pdf-converter.html seems to have a command line option.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38765

According to MS you can save as/to PDF in Word 2010 without add-ons; Word 2007 needs an add-on, see here for VBScript code. In either case, something like

objDoc.SaveAs <FullPathToOutputFile>, wdFormatPDF

should do the trick without involving a 'printer'.

For antique versions of Word the options are (in order of effort/gain ratio):

  1. update Word
  2. Record a macro of printing to the PDF printer and port the code to VBScript

Upvotes: 0

Related Questions