A
Alan
I am running the VBA code below in Excel 2002 SP3 under Windows XP
SP3. I received this error:
Run-time error '-2147417848(80010108)'
Automation error
The object invoked has disconnected from its clients
at this line of code:
MsgBox "The document " & FileName(WordDoc.name) & " has printed",
vbInformation, "PDF Print Status"
I Googled this error and found a few threads, but none of the seem to
relate to the problem I am having.
Anbody have any ideas what is going on?
The code, which resides in Excel, is trying to use the PDFCreator
application to print a Word file to PDF. To run it, you have to have
PDFCreator installed and have the Reference to PDFCreator checked in
the VBE.
Thanks, Alan
Option Explicit
Sub TESTPrintWordToPDF()
Debug.Print "Starting print of test.doc"
PrintWordToPDFCreator "C:\Documents and Settings
\thomasja.DLVASTRIKE\Desktop\PPT Compat\Excel\test.doc"
Debug.Print "Starting print of References.doc"
PrintWordToPDFCreator "C:\Documents and Settings
\thomasja.DLVASTRIKE\Desktop\PPT Compat\Excel\References.doc"
Debug.Print "Starting print of Issues.doc"
PrintWordToPDFCreator "C:\Documents and Settings
\thomasja.DLVASTRIKE\Desktop\PPT Compat\Excel\Issues.doc"
End Sub
Sub PrintWordToPDFCreator(WordDocPath As String)
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String, sPDFPath As String
Dim pos As Integer, sWordName As String
Dim sPrinter As String
Dim bRestart As Boolean
Dim bBkgrndPrnt As Boolean
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Debug.Print "setting file/path name"
' Set file and path name
sWordName = FileName(WordDocPath)
pos = InStr(1, sWordName, ".doc")
If (pos = 0) Then
DisplayErrorMessage (sWordName & " is not a Word document")
Exit Sub
Else
sPDFName = Replace(sWordName, ".doc", ".pdf")
sPDFPath = FolderName(WordDocPath)
End If
Debug.Print "checking file existence"
If Not CheckFileExist(WordDocPath) Then
MsgBox "The file " & WordDocPath & " does not exist"
Exit Sub
End If
Debug.Print "setting Word objects"
Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open(WordDocPath)
'Activate error handling, capture properties and set req'd
settings
'On Error GoTo EarlyExit
Debug.Print "setting printer props"
With WordApp
sPrinter = CStr(.ActivePrinter)
.ActivePrinter = "PDFCreator"
bBkgrndPrnt = .Options.PrintBackground
.Options.PrintBackground = False
.ScreenUpdating = False
End With
'Check if PDFCreator is already running and attempt to kill the
process if so
Debug.Print "Check if PDFCreator is already running"
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing
process
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
Debug.Print "Assigning PDF job settings"
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
Debug.Print "Printing Word doc to PDF"
'Print document to PDF
WordDoc.PrintOut copies:=1
Debug.Print "Checking printer queue"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
Debug.Print "Setting cPrinterStop = False"
'pdfjob.cPrinterStop = False
'Wait until the file shows up before closing PDF Creator
Debug.Print "Waiting until PDF file is created"
Do
DoEvents
Loop Until Dir(sPDFPath & sPDFName) = sPDFName
' Close Word document
Debug.Print "Closing Word document"
WordDoc.Close SaveChanges:=False
Cleanup:
'On Error Resume Next
Debug.Print "Cleaning up . . . "
'Release objects and terminate PDFCreator
MsgBox "The document " & FileName(WordDoc.name) & " has printed",
vbInformation, "PDF Print Status"
DoEvents
pdfjob.cClose
Shell "taskkill /f /im PDFCreator.exe", vbHide
Set pdfjob = Nothing
'Reset all application settings to user's original settings
With WordApp
.ScreenUpdating = True
.ActivePrinter = sPrinter
.Options.PrintBackground = bBkgrndPrnt
End With
' WordApp.Quit
Set WordApp = Nothing
Set WordDoc = Nothing
Debug.Print "All done"
Exit Sub
EarlyExit:
'On Error Resume Next
Debug.Print "Exiting early . . . "
'Inform user of error, and go to cleanup section
MsgBox "There was an error encountered. PDFCreator has" & vbCrLf
& _
"has been terminated. Please try again.", _
vbCritical + vbOKOnly, "Error"
' Resume Cleanup
Set WordDoc = Nothing
Debug.Print "I have exited early"
End Sub
SP3. I received this error:
Run-time error '-2147417848(80010108)'
Automation error
The object invoked has disconnected from its clients
at this line of code:
MsgBox "The document " & FileName(WordDoc.name) & " has printed",
vbInformation, "PDF Print Status"
I Googled this error and found a few threads, but none of the seem to
relate to the problem I am having.
Anbody have any ideas what is going on?
The code, which resides in Excel, is trying to use the PDFCreator
application to print a Word file to PDF. To run it, you have to have
PDFCreator installed and have the Reference to PDFCreator checked in
the VBE.
Thanks, Alan
Option Explicit
Sub TESTPrintWordToPDF()
Debug.Print "Starting print of test.doc"
PrintWordToPDFCreator "C:\Documents and Settings
\thomasja.DLVASTRIKE\Desktop\PPT Compat\Excel\test.doc"
Debug.Print "Starting print of References.doc"
PrintWordToPDFCreator "C:\Documents and Settings
\thomasja.DLVASTRIKE\Desktop\PPT Compat\Excel\References.doc"
Debug.Print "Starting print of Issues.doc"
PrintWordToPDFCreator "C:\Documents and Settings
\thomasja.DLVASTRIKE\Desktop\PPT Compat\Excel\Issues.doc"
End Sub
Sub PrintWordToPDFCreator(WordDocPath As String)
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String, sPDFPath As String
Dim pos As Integer, sWordName As String
Dim sPrinter As String
Dim bRestart As Boolean
Dim bBkgrndPrnt As Boolean
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Debug.Print "setting file/path name"
' Set file and path name
sWordName = FileName(WordDocPath)
pos = InStr(1, sWordName, ".doc")
If (pos = 0) Then
DisplayErrorMessage (sWordName & " is not a Word document")
Exit Sub
Else
sPDFName = Replace(sWordName, ".doc", ".pdf")
sPDFPath = FolderName(WordDocPath)
End If
Debug.Print "checking file existence"
If Not CheckFileExist(WordDocPath) Then
MsgBox "The file " & WordDocPath & " does not exist"
Exit Sub
End If
Debug.Print "setting Word objects"
Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open(WordDocPath)
'Activate error handling, capture properties and set req'd
settings
'On Error GoTo EarlyExit
Debug.Print "setting printer props"
With WordApp
sPrinter = CStr(.ActivePrinter)
.ActivePrinter = "PDFCreator"
bBkgrndPrnt = .Options.PrintBackground
.Options.PrintBackground = False
.ScreenUpdating = False
End With
'Check if PDFCreator is already running and attempt to kill the
process if so
Debug.Print "Check if PDFCreator is already running"
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing
process
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
Debug.Print "Assigning PDF job settings"
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
Debug.Print "Printing Word doc to PDF"
'Print document to PDF
WordDoc.PrintOut copies:=1
Debug.Print "Checking printer queue"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
Debug.Print "Setting cPrinterStop = False"
'pdfjob.cPrinterStop = False
'Wait until the file shows up before closing PDF Creator
Debug.Print "Waiting until PDF file is created"
Do
DoEvents
Loop Until Dir(sPDFPath & sPDFName) = sPDFName
' Close Word document
Debug.Print "Closing Word document"
WordDoc.Close SaveChanges:=False
Cleanup:
'On Error Resume Next
Debug.Print "Cleaning up . . . "
'Release objects and terminate PDFCreator
MsgBox "The document " & FileName(WordDoc.name) & " has printed",
vbInformation, "PDF Print Status"
DoEvents
pdfjob.cClose
Shell "taskkill /f /im PDFCreator.exe", vbHide
Set pdfjob = Nothing
'Reset all application settings to user's original settings
With WordApp
.ScreenUpdating = True
.ActivePrinter = sPrinter
.Options.PrintBackground = bBkgrndPrnt
End With
' WordApp.Quit
Set WordApp = Nothing
Set WordDoc = Nothing
Debug.Print "All done"
Exit Sub
EarlyExit:
'On Error Resume Next
Debug.Print "Exiting early . . . "
'Inform user of error, and go to cleanup section
MsgBox "There was an error encountered. PDFCreator has" & vbCrLf
& _
"has been terminated. Please try again.", _
vbCritical + vbOKOnly, "Error"
' Resume Cleanup
Set WordDoc = Nothing
Debug.Print "I have exited early"
End Sub