Convert Word Document to PDF With a Macro (VBA)
This macro creates Pdf for the active\current document and places it into the same folder, where the document is saved. The Code uses PDFMaker to convert Word Document (.doc) to pdf.
I am using Word 2003 with Adobe 8.0. This macro code also works in Word 2007.
Sub ConvertToPDF()
' ----------------------------------------------------
' ConvertToPDFWithLinks - convert the current document
' to a PDF file, in the same folder as the document
' ----------------------------------------------------
Dim pdfname, i, a
Dim pmkr As AdobePDFMakerForOffice.PDFMaker
Dim stng As AdobePDFMakerForOffice.ISettings
With ActiveDocument
'The document must be saved - so save it!
.Save
Set pmkr = Nothing ' locate PDFMaker object
For Each a In Application.COMAddIns
If InStr(UCase(a.Description), "PDFMAKER") > 0 Then
Set pmkr = a.Object
Exit For
End If
Next
If pmkr Is Nothing Then
MsgBox "Cannot Find PDFMaker add-in", vbOKOnly, ""
Exit Sub
End If
'The original definition of pdfname displayed an error
'This one works
pdfname = Left(.Name, InStrRev(.Name, ".")) & "pdf"
' delete PDF file if it exists
If Dir(pdfname) <> "" Then Kill pdfname
pmkr.GetCurrentConversionSettings stng
stng.AddBookmarks = True ' make desired settings
stng.AddLinks = True
stng.AddTags = False
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False
'MsgBox "Starting Conversion", vbOKOnly, "Starting Conversion"
pmkr.CreatePDFEx stng, 0 'perform conversion
'MsgBox "Conversion complete", vbOKOnly, "Conversion done"
If Dir(pdfname) = "" Then ' see if conversion failed
MsgBox "Could not create " & pdfname, vbOKOnly, "Conversion failed"
End If
End With
End Sub
I am using Word 2003 with Adobe 8.0. This macro code also works in Word 2007.
Sub ConvertToPDF()
' ----------------------------------------------------
' ConvertToPDFWithLinks - convert the current document
' to a PDF file, in the same folder as the document
' ----------------------------------------------------
Dim pdfname, i, a
Dim pmkr As AdobePDFMakerForOffice.PDFMaker
Dim stng As AdobePDFMakerForOffice.ISettings
With ActiveDocument
'The document must be saved - so save it!
.Save
Set pmkr = Nothing ' locate PDFMaker object
For Each a In Application.COMAddIns
If InStr(UCase(a.Description), "PDFMAKER") > 0 Then
Set pmkr = a.Object
Exit For
End If
Next
If pmkr Is Nothing Then
MsgBox "Cannot Find PDFMaker add-in", vbOKOnly, ""
Exit Sub
End If
'
'This one works
pdfname = Left(.Name, InStrRev(.Name, ".")) & "pdf"
' delete PDF file if it exists
If Dir(pdfname) <> "" Then Kill pdfname
pmkr.GetCurrentConversionSettings stng
stng.AddBookmarks = True ' make desired settings
stng.AddLinks = True
stng.AddTags = False
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False
'MsgBox "Starting Conversion", vbOKOnly, "Starting Conversion"
pmkr.CreatePDFEx stng, 0 'perform conversion
'MsgBox "Conversion complete", vbOKOnly, "Conversion done"
If Dir(pdfname) = "" Then ' see if conversion failed
MsgBox "Could not create " & pdfname, vbOKOnly, "Conversion failed"
End If
End With
End Sub
Hi,
ReplyDeleteIm working on a very similar process to convert word to pdf. Im able to convert, I have a challenge if the word document you want to convert has the readonly flag set to true.
Can you share if you have workaround or guide if any ideas.
Thanks.