Library code snippets

Printing from the WebBrowser control

There are three methods for doing this.

1. Sending ^p key, using SendKeys, which is not really clean programming :P

2. Using the shell command, and rundll32

Shell "rundll32.exe C:\WINDOWS\SYSTEM\MSHTML.DLL,PrintHTML " & _ "http://www.developerfusion.com" , vbMinimizedFocus

3. Finally, method 3, which I think is the best method, and uses the built-in commands for the web-browser.

Private Sub PrintUserPage()
'navigate to the page you want
'you can delete this if you want to print the current page
WebBrowser1.Navigate ("http://www.developerfusion.com/")

DoEvents
'print
WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
'you can change OLECMDEXECOPT_DONTPROMPTUSER to OLECMDEXECOPT_PROMPTUSER if you wish

End Sub

Comments

  1. 26 Jul 2007 at 13:49

    Does anyone knoe how toI specify a filename for the print file when using the webbrowser control to print from VB6?

  2. 24 Jul 2007 at 14:13

    hy,

    i want so set the focus to a frame..

    what should i enter instead of <content> ? the frame name?

    whats about iframes?

    thanks

  3. 06 Jul 2007 at 10:32

    try this

    webBrowser1.ShowPrintDialog()



  4. 06 Jul 2007 at 10:31

    try this

     webBrowser1.ShowPrintDialog()



  5. 09 Mar 2005 at 15:24

    How can I print from WebBrower using other printer than default printer

  6. 08 Nov 2002 at 06:02

    That 3rd suggestion of yours worked like a charm! Thank you.


    However, now I've managed to show a page in the WebBrowser that has some positioned DIVs. Their positions are changed with a JavaScript function that runs when the page loads.


    And then I tried to print that layout and what got printed was the original layout, not the one shown in the WebBrowser. Out of curiosity I right-clicked the page and printed it using the command in the context menu and..... it worked.


    Have you ever tried this?
    Thank you in advance.

  7. 14 Oct 2002 at 08:09

    I found the code here would not work for me, because the HTML page had not completed loading before the execWB method was called. I just got error messages. After trying many options, I found the answer was to put the ExecWB command in a sub for the event DownloadComplete.


    Private Sub PrintUserPage
    Webbrowser1.Navigate ("http://www.yourfavouriteURL.com")
    End Sub


    Private Sub WebBrowser1DownloadComplete()
    WebBrowser1.ExecWB OLECMDID
    PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    End Sub

  8. 11 Jun 2002 at 06:56

    This looks like the solution I have been looking for. But unfortunately, the shelling option doesn't print... (not sure why), but how do I code this into a VB.NET web page?


    Thanks

  9. 26 Dec 2001 at 13:51

    With reference to printing the webbrowser content. Sometimes you will have just one frame that you wish to print! So you need to know the frame and set the focus to that frame!


     WebBrowser1.Document.parentWindow.<content>.focus
     WebBrowser1.ExecWB OLECMDIDPRINT, OLECMDEXECOPTDONTPROMPTUSER

  10. 01 Jan 1999 at 00:00

    This thread is for discussions of Printing from the WebBrowser control.

Leave a comment

Sign in or Join us (it's free).