Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 315657 - Last Review: May 13, 2003 - Revision: 2.2
HOW TO: Print the Contents of a WebBrowser Control from Visual Basic
This article was previously published under Q315657
This article describes how to print the contents of an HTML document if the HTML control is embedded in a Visual Basic application by using the WebBrowser control.
Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you will need.
- Internet Explorer 4.0 or later
- A working knowledge of Visual Basic 6.0 application development for Windows
Embedding a WebBrowser Control in a Visual Basic Form
- Start the Visual Basic 6.0 development environment, and then create a new Standard EXE project.
- On the Project menu, click Components. Select the Microsoft Internet Controls check box, and then click OK to add it to the toolbox.
- Use the new icon in the toolbox to create a control named WebBrowser1 on Form1.
- On the View menu, click Code.
- Add the following code to the form to load an URL into the browser when the program starts. The actual URL is not significant in this example; replace it with any valid URL that you want to use:
Private Sub Form_Load()
WebBrowser1.Navigate2 "http://www.microsoft.com"
End Sub
- Run the project. The WebBrowser control appears in the form and displays the Web page that you specified as the first parameter for the Navigate2 method.
Printing from the WebBrowser Control
- Add a command button named Command1 to the form.
- Double-click Command1 to add a click event handler. Edit the code as follows:
Private Sub Command1_Click()
WebBrowser1.ExecWB OLECMDID_PRINT, _
OLECMDEXECOPT_PROMPTUSER, _
0, 0
End Sub
- Run the project. After the Web page has been loaded, click Command1. A dialog box appears with printer options. Click the appropriate printer, and then click OK. The document in the WebBrowser control is printed.
- If you do not want the dialog box to appear, change the second parameter of the ExecWB call to OLECMDEXECOPT_DONTPROMPTUSER. This causes the document to be sent to the printer without further user intervention. However, printing without prompting the user is not supported on Internet Explorer 5 (see the "Troubleshooting" section in this article).
Troubleshooting
If you use the OLECMDEXECOPT_PROMPTUSER option, it is not possible to determine whether the user clicked
OK to print the document or
Cancel to cancel printing. Internet Explorer 4.0 supports both OLECMDEXECOPT_PROMPTUSER and OLECMDEXECOPT_DONTPROMPTUSER. However, OLECMDID_DONTPROMPTUSER is ignored in Internet Explorer 5, because printing is considered to be a security issue; a Web page should not have the ability to start a print job without confirmation from the user. In Internet Explorer 5.5 and later, the print job is completed without user confirmation.
For more information about printing with the WebBrowser control, see the following MSDN article:
ExecWB is supported in Internet Explorer 4.0 and later, but it is not supported in Internet Explorer 3.0. For additional information about printing in Internet Explorer 3.0, click the article number below
to view the article in the Microsoft Knowledge Base:
156732Â
(http://kbalertz.com/Feedback.aspx?kbNumber=156732/EN-US/
)
HOWTO: Print from the Microsoft WebBrowser Control
APPLIES TO
- Microsoft Internet Explorer 5.0
- Microsoft Internet Explorer 4.0 128-Bit Edition
- Microsoft Internet Explorer 5.5
- Microsoft Internet Explorer 5.0 for Macintosh
- Microsoft Internet Explorer 4.0 for Macintosh
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
| kbhowto kbhowtomaster KB315657 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please