HOWTO: Print from the Microsoft Web Browser Control

Last reviewed: January 19, 1998
Article ID: Q156732
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Edition, version 5.0
  • Internet Explorer 3.0, 3.01, 3.02, 4.0, 4.01
  • Internet Client SDK, version 4.0, 4.01

SUMMARY

The Web Browser component of Internet Explorer 3.x and 4.x does not support a Print method. You can still print the contents of the Web Browser control using one of these methods:

  • Set focus to the Web Browser control and send a key combination of CTRL+P.

    -or-

  • Call the GetDocument() method that returns an IDispatch*. Using the IDispatch*, call QueryInterface() on IID_IOleCommandTarget. With the object pointer returned, call Exec(NULL, OLECMDID_PRINT, 0, NULL,NULL).

MORE INFORMATION

The following code can be used to cause the Web Browser control to print its contents. In this example, m_ctlWebBrowser is an instance of the wrapper class generated by Class Wizard for the control. m_ctlWebBrowser is contained in a dialog and initialized in the OnInitDialog function. A "Print" button on the dialog is wired to an OnPrint function. The "Print" button is enabled in response to the NavigateComplete event of the Web Browser control.

Sample Code

   BOOL CAboutDlg::OnInitDialog()
   {
       CDialog::OnInitDialog();

       m_ctlWebBrowser.Navigate(_T("http://www.microsoft.com"),
                                NULL, NULL, NULL, NULL);
       return TRUE;
   }

Method #1

   void CAboutDlg::OnPrint()
   {
       CWnd* pWnd = GetFocus ();

       m_ctlWebBrowser.SetFocus ();
       // send Ctrl-P
       keybd_event (VK_CONTROL, 0, 0, 0);
       keybd_event ('P', 0, 0, 0);

       // set focus to control that previously had focus
       if (pWnd);
           pWnd->SetFocus ();
   }

Method #2

   // DOCOBJ.H comes with the Internet Client SDK and is installed by
   // default in the "\INetSDK\Include" directory
   #include <docobj.h>

   void CAboutDlg::OnPrint()
   {
       LPDISPATCH lpDispatch = NULL;
       LPOLECOMMANDTARGET lpOleCommandTarget = NULL;

       lpDispatch = m_ctlWebBrowser.GetDocument();
       ASSERT(lpDispatch);

       lpDispatch->QueryInterface(IID_IOleCommandTarget,
                                  (void**)&lpOleCommandTarget);
       ASSERT(lpOleCommandTarget);

       lpDispatch->Release();

       // print contents of web browser control
       lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, 0, NULL,NULL);

       lpOleCommandTarget->Release();
   }

REFERENCES

Internet Client SDK Online documentation.

Keywords          : MfcOLE kbprint kbhowto
Technology        : kbole kbmfc
Version           : 4.2
Platform          : NT WINDOWS


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 19, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.