• Launch and Print a PDF from Word (VBA)

    • This topic has 2 replies, 2 voices, and was last updated 19 years ago.
    Author
    Topic
    #431801

    I have the following code which worked fine until a change of printer drivers. The PDF still opens however will not print (as it used to). Any ideas please ?

    Dim prog As String, fn As String

    ‘find Acrobat
    prog = Chr(34) & “C:Program FilesAdobeAcrobat 7.0ReaderAcroRd32.exe” & Chr(34)

    ‘find the pdf to print
    fn = “test.pdf”

    ‘add quotes
    fn = Chr(34) & fn & Chr(34)

    ‘fire up Acrobat and print
    Shell prog & “/p/h ” & fn, vbNormalFocus

    Viewing 0 reply threads
    Author
    Replies
    • #1011507

      Try using ShellExecute:

      Put the following declarations at the beginning of a module:

      Private 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

      Private Const SW_SHOWNORMAL = 1

      Public Sub PrintPDF(fn As String)
      Dim lngResult As Long
      lngResult = ShellExecute(0&, "Print", fn, 0&, 0&, SW_SHOWNORMAL)
      If lngResult <= 32 Then
      MsgBox "Something went wrong.", vbExclamation
      End If
      End Sub

      To print test.pdf, use

      Call PrintPDF("test.pdf")

      or

      Dim fn As String
      fn = "test.pdf"
      Call PrintPDF(fn)

      • #1011517

        Thanks Hans. That code works in background which is far neater. I didn’t receive the pdf I wanted – instead an error page printed “Your application needs to be configured for printing to a PostScript Language Level 2 printer”. My work colleague said he’ll take a look in the PostScript Printer options tomorrow and then it should work a treat.

    Viewing 0 reply threads
    Reply To: Launch and Print a PDF from Word (VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: