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
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):
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
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.
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.