• Printing a PDF from VB (VB 6.0 / W98)

    Author
    Topic
    #374673

    I need to print a PDF file from within my VB application, so what kind of code do I need attached to my command button OnClick event? (I am assuming that the computer already has the Adobe Reader program installed — if not, I need a graceful way back to my VB form.

    Viewing 0 reply threads
    Author
    Replies
    • #606811

      The best way to do this is to first check to see if the file association is in place. If so, Reader is installed. Use the FindExecutable API function for that.
      Next, use the ShellExecute API function with the “Print” operation (parameter 2).

      You can also reference the pdf.ocx/tlb library and use it’s property SRC to name the file and the method Print to print the pdf.

      I like the ShellExecute technique better.

      p.s. the pdf.ocx has no documentation except what you can find via google, etc., and it is quiet unstable (at least on W2k).

      Search this forum for more discussion on the pdf.ocx control.

      • #606847

        Hi Kevin,

        Thanks for the advice. I got the first part working great – it detects the presence of the Acrobat Reader. If not, I change my command button caption to “you need to install Acrobat Reader.”

        However, being a novice at VB 6, I am struggling with exactly how to call the ShellExecut function to print the file.

        I have this so far, but I immediately get a “expected expression” error message.

        Call ShellEx(hwnd,”Print”,”C:Program FilesAPI VisionInstructions.PDF”,NULL,,)

        I kind of expected it to go off and print the file.

        (Frankly, I really don’t understand the “hwnd” part.)

        • #606851

          The ShellExecute function needs a window handle as the first parameter so it knows where to return any errors. Use the current window (your app, a userform, the desktop, etc.). To get the current window handle, use GetActiveWindow

          hWnd = GetActiveWindow() — Dim hWnd as long

          The function expects all five parameters and you are missing one.

          Throw these values at the function:

          hWnd (you’re fine here if you get a valid window handle); “Print” (you absolutely nailed this one), the filename (full path as you have done), 0& (that’s zero ampersand — something about a directory), 0& (another of the same — a paramaters parameter) and last but not least: 1 — the Show parameter. 1 says show normal which I think means don’t show the app, in this case Acrobat Reader.

          Call ShellEx(hwnd,”Print”,”C:Program FilesAPI VisionInstructions.PDF”,0&,0&,1)

          • #606861

            Hi again,

            I’ll be darned if it didn’t work perfectly! But, of course, that’s never the end of it.

            I notice that upon finishing printing I’m left with the Adobe Acrobat task running (minimized on the Task Bar). Is there some way to detect that it is done and kill the Acrobat task?

            Thanks once more, you have been very gracious with your help.

            • #607096

              Chuck,

              If it leaves Acrobat running it didn’t work perfectly! Ha! You should not have to bother with killing the process. Acrobat should load (in the background — you should not see the splash screen, print the file and exit. That’s how it works here.

              Is this what your API declaration looks like:

              Public 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

            • #607183

              Hi Kevin,

              Correction. The instance of Acrobat or Acrobat Reader remains on the Task Bar when finished printing.

              I suppose I can live with it. Thanks for all your help.

            • #607185

              Weird. I have both installed on my PC (Acrobat 4.0 and Reader 5.0) and it works fine. Oh well. You could explore the pdf.ocx/tlb solution, but I think that always displays the splash screen (and has other problems as mentioned earlier).

    Viewing 0 reply threads
    Reply To: Printing a PDF from VB (VB 6.0 / W98)

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

    Your information: