Results 1 to 8 of 8
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Printing a PDF from VB (VB 6.0 / W98)

    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.

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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.)

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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)
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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.

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  7. #7
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a PDF from VB (VB 6.0 / W98)

    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).
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

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