Printing Web Pages like Internet Explorer


To implement the print function i.e. when we click on the print button in the toolbar no print setup dialog will be displayed and the application will automatically start printing the document. This feature is also present in Microsoft Word. To implement this feature in your application just use the code given below.

void CMyBrowser::OnPrint() 

	m_pDisp = m_WebBrowser2.GetDocument(); //get the IDispatch interface pointer

	m_pDisp->QueryInterface(IID_IOleCommandTarget, (LPVOID*)&pCmdTarg); //query for olecommandtarget interface

	pCmdTarg->Exec(NULL, //call the olecommandtarget's Exec method

	if (pCmdTarg) 
		pCmdTarg->Release(); // release document's command target

	if (m_pDisp) 
		m_pDisp->Release(); // release document's dispatch interfac

Get the IDispatch interface pointer by calling the Webbrowser controls GetDocument method. Then Query for the IOleCommandTarget interface. After getting the IOleCommandTarget pointer then call that interface's Exec method passing the OLECMDID_PRINT as the first parameter. For detailed explanation for Ole and the Interfaces involved in this piece of code please refer to visual c++ help. If you have any comments and suggestions or if you find any bugs in this article let me know.

Updated on May 23 1998


  • m_pDisp -- undeclared identifier

    Posted by dacylet on 03/01/2006 11:50am

    Would someone tell me what is this type? What should I declare it as? Thank you

  • Name to print file as

    Posted by Legacy on 06/18/2003 12:00am

    Originally posted by: Deepthi

    I am printing the file using execwb method and have set my default printer to acrobat distiller.It opens up the dailog box to name the file as and the path it needs to save to.Can I send these parameters without having the dailog box to open up.


  • The only idiot with a question

    Posted by Legacy on 03/03/2003 12:00am

    Originally posted by: Budlo

    Hey guru's,

    I'm having a little problem. The code given works great. I thought that m_WebBrowser2.ExecWB(OLECMDID_PRINT, 1, NULL, NULL);works even better. Now here's the problem:

    I want my program to print the page as is, without the address or pagenumber bullshit. So can anyone help me?


  • whats about dhtml control using IOleCommandTarget

    Posted by Legacy on 02/10/2003 12:00am

    Originally posted by: bhavani

    Instead of WEBBROWSER object i have taken DHTML control.How the print and printview functionality implements using IOleCommanadTarget.Please anyone guide me.

  • HTML Printing problem

    Posted by Legacy on 04/24/2002 12:00am

    Originally posted by: Bahar

    Y doesnt the following code work.
    I'm trying to print a html file.Though the file is seen in the print queue no output is seen in the prinout.
    Plz help.
    thanks in advance

    // Get the default printer.

    DEVMODE* dm = (DEVMODE*)::GlobalLock(dlg.m_pd.hDevMode);

    HDC hdc = dlg.CreatePrinterDC();

    CDC dc;
    dc.Attach( hdc );
    if ( dc )

    DOCINFO docinfo;
    memset(&docinfo, 0, sizeof(docinfo));

    docinfo.cbSize = sizeof(docinfo);
    docinfo.lpszDocName =htmlDocumentName ;



    //CGdiObject* pOldFont = dc.SelectStockObject(SYSTEM_FONT);
    //dc.TextOut(50, 50, _T("Hello World!"), 12);
    TR_PRINT(_T("Orientation %d "),dm->dmOrientation);





  • How to open the page from the cusomizes app

    Posted by Legacy on 11/12/2001 12:00am

    Originally posted by: Ram

    I have built customized web browser application using the CHtmlView and the print functionality provided in your article. However whenever I try to browse a page , it opens the url in a new IE browser window. I would like to open it from the custom MFC application browser window. Is there a way to do this ?

  • How to intercept the contents of Homepage before showing in the IE browser without proxy?

    Posted by Legacy on 02/20/2001 12:00am

    Originally posted by: Yung-Ming Kuo

    Dear friends:

    I want to intercept and get the contents(including text and image) of homepage from Web server ,before it shows the IE browser(version 5.0).But it is not through proxy.

    And i have ability to prevent HTML document from showing the browser.

    Could you offer some suggestion or source code sample to solve my question?

    Thank you ^_^

    by Kuo

  • make a form print from a print button on page

    Posted by Legacy on 08/08/2000 12:00am

    Originally posted by: Alan Payne

    Can any one help i want a form to print by clicking a print button in a form thanks Alan

  • ExecWB and Debugger

    Posted by Legacy on 10/05/1999 12:00am

    Originally posted by: G. Bremer

    Printing Web Pages using ExecWB works fine - if the App is run separately. However, if it is run under Debugger control (VS6/SP3) the behaviour is quite strange (such as the Printer Dialog always popping up regardless the Mode specified) and it actually doesn't print. The errCode returned by ExecWB is also not very helpful. Does anyone have an Idea how this can be avoided?

  • CWebBrowser2::GetDocument fails!

    Posted by Legacy on 09/29/1999 12:00am

    Originally posted by: Simon Hill

    I have a CWebBrowser control which has successfully
    navigated to an html file and I can see this file.
    However when I try to print the file the program ASSERTS
    at the CWebBrowser2::GetDocument() stage.


    Simon Hill

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Thanks to the Internet of Things (IoT), physical assets are turning into participants in real-time global digital markets. The countless types of assets around us will become as easily indexed, searched and traded as any online commodity. While some industries will be tougher to transform than others – those with physical limitations, such as manufacturing, will be harder to digitize – untold economic opportunities exist for growth and advancement. Our research shows this will create a new "Economy …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date