Hi,
I am using PDFCreator in MS access to print a report directly to PDF using VB code. It prints the document and then MS Access hangs up giving an error message "Action cannot be completed beacuse the other program is busy". The message window is constsntly blinking and I am unable to click on anything.
Below is the code I am using. Any help will be much appreciated.
----------------------------------------------------------
Sub PrintAccessReportToPDF_Early()
'Author : Amit Patel
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from http://sourceforge.net/projects/pdfcreator/ )
' Designed for early bind, set reference to PDFCreator
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim sPrinterName As String
Dim sReportName As String
Dim lPrinters As Long
Dim lPrinterCurrent As Long
Dim lPrinterPDF As Long
Dim prtDefault As Printer
On Error GoTo Err_Handler_PrintAccessReportToPDF_Early
'/// Change the report and output file name here! ///
sReportName = "Questionnaire Cover Letter"
sPDFName = sReportName & "_" & Date & ".pdf"
sPDFPath = "C:\Documents and Settings\All Users\Desktop\"
'Resolve index number of printers to allow changing and preserving
sPrinterName = Application.Printer.DeviceName
lPrinterPDF = -1
'On Error Resume Next
For lPrinters = 0 To (Application.Printers.Count - 1)
Set Application.Printer = Application.Printers(lPrinters)
Set prtDefault = Application.Printer
Select Case prtDefault.DeviceName
Case Is = sPrinterName
lPrinterCurrent = lPrinters
Case Is = "PDFCreator"
lPrinterPDF = lPrinters
Case Else
'do nothing
End Select
Next lPrinters
'On Error GoTo 0
'Change the default printer
If lPrinterPDF <> -1 Then
Set Application.Printer = Application.Printers(lPrinterPDF)
Set prtDefault = Application.Printer
Else
MsgBox "Error setting PDFCreator as default printer", vbCritical, "ASQ Error: PDFCreator"
GoTo ExitNow
End If
'Start PFF Creator
Set pdfjob = New PDFCreator.clsPDFCreator
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Print the document to PDF
DoCmd.OpenReport (sReportName)
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until PDF creator is finished then release the objects
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
'Reset the (original) default printer and release PDF Creator
Set Application.Printer = Application.Printers(lPrinterCurrent)
ExitNow:
Set pdfjob = Nothing
Exit Sub
Err_Handler_PrintAccessReportToPDF_Early:
Set Application.Printer = Application.Printers(lPrinterCurrent)
Set pdfjob = Nothing
GoTo ExitNow
End Sub
-----------------------------------------------------------