Background Printing From VBA Macro Crashes MS Word 2000

The Amyuni PDF Converter is our Printer Driver that enables you to convert any documents to PDF format. If you have any questions about installing and using the Amyuni PDF Converter please post them here.
Post Reply
craigh
Posts: 3
Joined: Fri Jan 26 2007

Background Printing From VBA Macro Crashes MS Word 2000

Post by craigh »

Hi,

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
If I set 'ActiveDocument.PrintOut Background' to 'True' MS Word no longer craches but then I get an error at 'docPDF.Open'.

I don't have any problems with MS Word XP and 2003.

Is this a bug or something I'm doing wrong?

Regards,

Craig
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

I don't have Ms Word 2000 myself to try it I have XP but which version of the PDF Converter are you using?
craigh
Posts: 3
Joined: Fri Jan 26 2007

Post by craigh »

The latest. V2.5?
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello Graigh

I tried to print a Word 2000 document on Windows XP using VBA code and i was able to print the document properly while setting the Background printing option of Word to False.

I am using version 2.51-d of the PDF Converter but I am sure this used to work with older versions of the driver.

I suggest however that you try the latest version if you don't have it or please send your code to support@amyuni.com so our Technical Support team can debug it.

Regards,
Joanna
craigh
Posts: 3
Joined: Fri Jan 26 2007

Post by craigh »

Hi,

I'm using V2.51-d and when running the code behind MS Word 2000 on a number of Windows XP PCs MS Word crashes.

I'll send the code to your technical team.

Regards,

Craig
Post Reply