VBA code shellexecute not sending PDF doc to printer

219 Views Asked by At

I have inherited an Access program that worked fine for at least 10 years.

One of the tasks it performs - printing a batch of 10 to 100 reports and associated PDF documents in collated manner.

If the associated document is Word or Excel - everything goes fine.

When it comes to printing PDF, the report is printed but no PDF. No errors, no printout. Just disappears like into the Black Hole.

I need the Acrobat hidden or, at least, minimized.

This code opens Acrobat. If I click Print icon it prints normally.

lngRetVal = ShellExecute& (lngHwnd, "open", strPath, vbNullString, vbNullString, SW_SHOWNORMAL)

Not an option!

If I try to invoke "print" instead of "open" - no Acrobat opening, no print job in the queue, no printed documents, no errors.

lngRetVal = ShellExecute& (lngHwnd, "Print", strPath, vbNullString, vbNullString, SW_HIDE )

So now it's narrowed down to "Print" that does nothing, not sending command to printer.

Not only that, but if I use "Print" and SW_SHOWNORMAL Acrobat also wouldn't open and gives me a message "Acrobat quit unexpectedly....".

I have (and all other computers that run this Access app) Acrobat library 10 in the references.

Office 365.

Code has been adopted to 64 bit system in 2013.

The problem started on September 14.

Somebody suggested that it might be connected to Win update.

I did the rollback - no change.

Doesn't matter if:

  • printer is network or local

  • which computer the access app is running on

I have tried to get help in Microsoft Community support and still not an inch of progress.

Please help.

1

There are 1 best solutions below

2
vbakim On

Here is an example code, will loop through all PDF files in that folder and print them.

#If VBA7 Then
    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hWnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, _
        ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
#Else
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
        ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If

Sub PrintPDFFiles()
    Dim FolderPath As String, FileName As String, PDFPath As String
    Dim hWnd As LongPtr, Result As LongPtr

    'Your PDF files are located
    FolderPath = ThisWorkbook.Path & "\"

    hWnd = Application.hWnd

    ' Loop All files in the folder
    FileName = Dir(FolderPath & "*" & ".pdf")
    Do While FileName <> ""
        PDFPath = FolderPath & FileName
        Result = ShellExecute(hWnd, "Print", PDFPath, vbNullString, vbNullString, 3)

        If Result <= 32 Then
            MsgBox "Failed to print " & PDFPath
        Else
            ' Adjust the duration as needed
            Application.Wait Now + TimeValue("00:00:05")
            Call Shell("taskkill /f /im AcroRd32.exe", vbHide)
        End If
        FileName = Dir
    Loop
End Sub