When I run the following code in MS Word 2000 it causes MS Word to crash. I notice however that in the list of processes in Task Manager that Winword.exe is still present.
Code: Select all
Option Explicit
Public Sub PDFPrint()
Dim strActivePrinterMemory As String
Dim cdiPDFPrinter As Object
Dim docPDF As Object
Const PDF_DRIVER_NAME = "Amyuni PDF Converter"
Const PDF_FILE_PRINT_OPTION_RESET = 0
Const PDF_FILE_PRINT_OPTION_NO_PROMPT = 1
Const PDF_FILE_PRINT_OPTION_USE_FILE_NAME = 2
Const PDF_FILE_PRINT_OPTION_CONCATENATE = 4
Const PDF_FILE_PRINT_OPTION_DISABLE_COMPRESSION = 8
Const PDF_FILE_PRINT_OPTION_EMBED_FONTS = 16
Const PDF_FILE_PRINT_OPTION_BROADCAST_MESSAGES = 32
Const PDF_PAPER_SIZE_A4 = 9
Const PDF_PAPER_ORIENTATION_PORTRAIT = 1
Const PDF_PERM_OWNER_PASSWORD = "Test"
Const PDF_PERM_USER_ALLOW_PRINTING = &H4
Const PDF_PERM_USER_ALLOW_CHANGING_DOC = 8
Const PDF_PERM_USER_ALLOW_COPYING = 16
Const PDF_PERM_USER_ALLOW_NOTES = &H32
Const PDF_COMP_256_COLOUR = &H800000
On Error GoTo Err_PDFPrint
'Initialise PDF printer
Set cdiPDFPrinter = CreateObject("CDIntfEx.CDIntfEx")
With cdiPDFPrinter
.DriverInit PDF_DRIVER_NAME
'Set margins to zero
.HorizontalMargin = 0
.VerticalMargin = 0
.SetDefaultConfig
'Set paper size & orientation
.PaperSize = PDF_PAPER_SIZE_A4
.Orientation = PDF_PAPER_ORIENTATION_PORTRAIT
End With
'Set PDF printer
strActivePrinterMemory = Application.ActivePrinter
If Not CBool(InStr(1, Application.ActivePrinter, PDF_DRIVER_NAME, vbTextCompare)) Then Application.ActivePrinter = PDF_DRIVER_NAME
'Print doc to PDF
With cdiPDFPrinter
'Set PDF page file name
.DefaultFileName = "C:\" & Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4) & ".pdf"
'Set options
.FileNameOptionsEx = PDF_FILE_PRINT_OPTION_NO_PROMPT + PDF_FILE_PRINT_OPTION_USE_FILE_NAME + PDF_COMP_256_COLOUR
'Print doc to PDF file
ActiveDocument.PrintOut Background:=False
'Reset FileNameOptions
.FileNameOptionsEx = PDF_FILE_PRINT_OPTION_RESET
End With
Set cdiPDFPrinter = Nothing
'Reset default printer
If Application.ActivePrinter <> strActivePrinterMemory Then Application.ActivePrinter = strActivePrinterMemory
'Create PDF document object
Set docPDF = CreateObject("CDIntfEx.Document")
'Activate advanced functions
docPDF.SetLicenseKey "???", "???"
'Open created document
docPDF.Open ("C:\" & Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4) & ".pdf")
'Set permissions
docPDF.Encrypt PDF_PERM_OWNER_PASSWORD, "", &HFFFFFFC0 + PDF_PERM_USER_ALLOW_COPYING + PDF_PERM_USER_ALLOW_PRINTING
'Set properties
docPDF.Subject = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
'Save finished document
docPDF.Save ("C:\" & Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4) & ".pdf")
Set docPDF = Nothing
Exit_PDFPrint:
On Error GoTo 0
Exit Sub
Err_PDFPrint:
Set docPDF = Nothing
Set cdiPDFPrinter = Nothing
Select Case Err.Number
Case Else:
MsgBox Err.Number & "-" & Err.Description, vbCritical + vbOKOnly, "Error message"
End Select
Resume Exit_PDFPrint
End Sub
I don't have any problems with MS Word XP and 2003.
Is this a bug or something I'm doing wrong?
Regards,
Craig