Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Mar 2001
    Location
    NSW, Australia
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Launch and Print a PDF from Word (VBA)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Launch and Print a PDF from Word (VBA)

    Try using ShellExecute:

    Put the following declarations at the beginning of a module:
    <code>
    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
    </code>
    To print test.pdf, use
    <code>
    Call PrintPDF("test.pdf")
    </code>
    or
    <code>
    Dim fn As String
    fn = "test.pdf"
    Call PrintPDF(fn)</code>

  3. #3
    Lounger
    Join Date
    Mar 2001
    Location
    NSW, Australia
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Launch and Print a PDF from Word (VBA)

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •