The code below is intended to, via Excel, convert a Word document to a PDF making use of the PDF Maker addin which is installed on the machine. This macro is only called if the machine is running Excel 2003. Newer versions make use of the ExportAsFixedFormat method, and that code is not shown here.
The code runs well except when it gets to the ".Close savechanges:=false" line. The PDF actually gets created, but at that point I get an error that reads "object invoked has disconnected from its client". Not sure why this would happen since it is still within the wdocDocument "with" statement. After the code fails the Word document is left open, of course.
What I've read suggests that it may be a binding problem, but I can't figure out how to fix it.
Any help will be appreciated. Thanks.
Private Sub ConvertToPDF(wdFileName As String, pdfFileName As String)
Dim wd As Word.Application
Dim wdocDocument As Word.Document
Dim WordAddin As Object
Dim x As Integer
Dim docConverter As AdobePDFMakerForOffice.pdfMaker
Dim pdfMakerSettings As AdobePDFMakerForOffice.ISettings
On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0
Set WordAddin = Nothing
For Each WordAddin In wd.COMAddIns 'Word.Application.COMAddIns
If InStr(UCase(WordAddin.Description), "PDFMAKER") > 0 Then
Set docConverter = WordAddin.Object
End If
Next WordAddin
If docConverter Is Nothing Then
MsgBox "Cannot find PDFMaker add-in on this machine. PDFs cannot be generated.", vbOKOnly, ""
End
End If
If FileExists(pdfFileName) Then
Kill pdfFileName
End If
Set wdocDocument = wd.Documents.Open(wdFileName)
With wdocDocument
docConverter.GetCurrentConversionSettings pdfMakerSettings
With pdfMakerSettings
.AddBookmarks = True
.AddLinks = True
.AddTags = True
.ConvertAllPages = True
.CreateFootnoteLinks = True
.CreateXrefLinks = True
.OutputPDFFileName = pdfFileName
.PromptForPDFFilename = False
.ShouldShowProgressDialog = False
.ViewPDFFile = False
End With
docConverter.CreatePDFEx pdfMakerSettings, 0
.Close savechanges:=False
wd.Quit
Set wdocDocument = Nothing
Set wd = Nothing
Set docConverter = Nothing
End With
If Not FileExists(pdfFileName) Then
MsgBox "There was a problem. The PDF was not created."
End If
End Sub
Bookmarks