Improve company productivity with a Business Account.Sign Up


Setting Document/Printer Properties with ie5 browser ocx or with html

Posted on 2001-08-22
Medium Priority
Last Modified: 2013-12-25
Setting Document/Printer Properties with ie5 browser ocx or with html

I want to print html forms in differen sizes, orintation with the browser control.

I tried three approaches:
1. size and orientation in the .html file
   example: <style> @page { size:21.0cm 14.5cm;landscape } </style>
   but this needs CSS2 support which the internet explorer does not
   -or is there another way?

2. application driven
   at the moment i have a small VB sample application that works quit well

       WebBrowser.Navigate (AnyUrl)

   Private Sub WebBrowser_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   End Sub

   except for the fact, that i have to configure the printer manually
   OLECMDEXECOPT_PROMPTUSER shows the printer dialog but i would rather use
   OLECMDEXECOPT_DONTPROMPTUSER and apply the settings programatically

   -can i change the settings  with VB or do i have to do this in C++ using COM?
   -does anyone hav an example.
   -i am a C++ programmer with some basic knowledge of MFC.
   -but i dont know how to handle IPrint::Print  with DEVMODE

3. default printer settings
   i also tried to change the printer properties (start/settings/printers)
   but the browser just does not use them.
   may be there is somewhere a template i could use
   internet explorer/ properties / general (tab) / accessibility (button) / user style sheet
   -how must a stylesheet look?
   -would it work like this? or are these stylesheets only for display?  


Yours Sincerely // Mit freundlichen Grussen

Theo Portmann

Atraxis AG
CH-8058 Zurich-Airport

Phone: +41 1 812 28 63
Telefax: +41 1 812 90 31

Question by:Baloon
  • 6
LVL 14

Accepted Solution

wsh2 earned 800 total points
ID: 6415728
Background Information From MSDN (which you have already implemented):

Q188271 HOWTO: Print Contents of the Web Browser Control From VB

HOWTO: Print Contents of the Web Browser Control From VB

The information in this article applies to:

Microsoft Visual Basic Learning Edition for Windows, versions 5.0, 6.0
Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0
Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0


The Web Browser control (version 3.x or 4.x) does not offer a direct Print method. This article demonstrates how to print the contents of the Web Browser control from Visual Basic.


Step-by-Step Example
Create a new Standard EXE project in Visual Basic. Form1 is created by default.

Select Components from the Project menu. Add a reference to the "Microsoft Internet Controls."

Add the following controls to Form1:

1 CommandButton
1 Web Browser

Add the following code to Form1:

      Option Explicit

      Private Sub Command1_Click()

         ' print the contents of the Web Browser control.
         WebBrowser1.ExecWB _

      End Sub

      Private Sub Form_Load()

         ' set the caption of the command button.
         Command1.Caption = "&Print"

         ' position the print command button at the top-left.
         Command1.Move 0, 0

         ' position the Web Browser control below the command button.
          WebBrowser1.Move 0, 750

          ' navigate to a Web site for testing purposes.
          WebBrowser1.Navigate ""

      End Sub

      Private Sub Form_Resize()

         ' when the form is resized, the Web Browser control should
         ' remain the same size as the form.
         WebBrowser1.Width = Form1.Width
         WebBrowser1.Height = Form1.Height

      End Sub

Press the F5 key to run the project. When the test site has been downloaded and rendered in the Web Browser, click the "Print" command button, and the contents of the Web Browser will be printed.

NOTE: Using version 3.x of the Web Browser control will result in a Print Dialog appearing when you attempt to print the content of the control. This is by design.
LVL 14

Expert Comment

ID: 6415769
However, this is very interesting.. Registry settings?.. Notice that they are using OLECMD, and NOT the Print() method now available with IEv5.5. Hmmmm...

From MSDN:

PRB: Programmatically Issuing Print Command May Not Print to Default Printer 

The information in this article applies to:

Microsoft Internet Explorer (Programming) versions 5.5, 5.5 Service Pack 1


IMPORTANT: This article contains information about editing the registry. Before you edit the registry, make sure you understand how to restore it if a problem occurs. For information about how to do this, view the "Restoring the Registry" Help topic in Regedit.exe or the "Restoring a Registry Key" Help topic in Regedt32.exe.

In Internet Explorer 5.5, when you issue a print command, the print job may not be sent to the default system printer. This occurs whether you click Print on the File menu or if you call the ExecWB method of the WebBrowser control.

When you select a printer in Internet Explorer (on the File menu, click Page Setup, click Printer, and choose a printer that is not the system default), your printer selection is stored in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\printer
After you make this selection, Internet Explorer continues to use the printer value that is stored in the registry.

WARNING: Using Registry Editor incorrectly can cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that problems resulting from the incorrect use of Registry Editor can be solved. Use Registry Editor at your own risk.

For information about how to edit the registry, view the "Changing Keys and Values" Help topic in Registry Editor (Regedit.exe) or the "Add and Delete Information in the Registry" and "Edit Registry Data" Help topics in Regedt32.exe. Note that you should back up the registry before you edit it. If you are running Windows NT or Windows 2000, you should also update your Emergency Repair Disk (ERD).

If you want to use the Microsoft Windows default printer when you host a WebBrowser control, you can use the code in this section to set this registry key to an empty string "", issue your print command, and then restore the value in the registry. Because there is no value under this key, Internet Explorer uses the default system printer.

NOTE: Make sure that you do not restore the registry value until after you are sure that the print job has finished; otherwise, you may restore the printer in the registry before Internet Explorer has actually read this key. This can occur when you print more complex Web pages.

To set the registry key to an empty string "", use the following Microsoft Visual Basic code:

    Dim wsh As Object
    Dim printer As String
    Const reg = "HKCU\Software\Microsoft\Internet Explorer\PageSetup\printer"
    Set wsh = CreateObject("WScript.Shell")
    printer = wsh.RegRead(reg)
    If Not printer = "" Then
        '   Clear the value
        wsh.RegWrite reg, ""
    End If

    ' NOTE: Because Internet Explorer may not have read the registry key
    ' yet, additional code may be required to ensure that Internet Explorer
    ' has read the registry key before you set it back to "".
    If Not printer = "" Then
        wsh.RegWrite reg, printer
    End If
    Set wsh = Nothing
LVL 14

Expert Comment

ID: 6415800
Checked the registy Internet Explorer PageSetup settings.. Printer Name, Margins, Page Header and Footer templates are there.. but no Paper size (sighs?) and orientation (which, it seems, has to be a function of Printer Name).  
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 14

Expert Comment

ID: 6415841
LVL 14

Expert Comment

ID: 6415849
Interesting reading (although I can't make sense of it yet.. lol):

Develop Your Own Browser
LVL 14

Expert Comment

ID: 6415861
Nevermind on that previous link.. nothing special in it.. <sigh>.

Expert Comment

ID: 6862685
Per recommendation, force-accepted by
Community Support Moderator
Experts Exchange

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As a person who answers a lot of questions, I often see code that could be simplified, made easier to read, and perhaps most importantly made easier to maintain if the code was modified to use the Select Case statement. This article explains how to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question