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

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com