Printing Web Pages like Internet Explorer
Posted
by Sriram Srinivasan
on August 7th, 1998
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()
{
LPOLECOMMANDTARGET pCmdTarg = NULL;
m_pDisp = m_WebBrowser2.GetDocument(); //get the IDispatch interface pointer
ASSERT(m_pDisp);
m_pDisp->QueryInterface(IID_IOleCommandTarget, (LPVOID*)&pCmdTarg); //query for olecommandtarget interface
ASSERT(pCmdTarg);
pCmdTarg->Exec(NULL, //call the olecommandtarget's Exec method
OLECMDID_PRINT,
0,
NULL,
NULL
);
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

Comments
m_pDisp -- undeclared identifier
Posted by dacylet on 03/01/2006 11:50amWould someone tell me what is this type? What should I declare it as? Thank you
ReplyName to print file as
Posted by Legacy on 06/18/2003 12:00amOriginally posted by: Deepthi
Hi,
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.
TIA
ReplyThe only idiot with a question
Posted by Legacy on 03/03/2003 12:00amOriginally posted by: Budlo
Replywhats about dhtml control using IOleCommandTarget
Posted by Legacy on 02/10/2003 12:00amOriginally posted by: bhavani
Hi,
ReplyInstead 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:00amOriginally 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
Bahar
CPrintDialog dlg(FALSE, PD_NOPAGENUMS | PD_NOSELECTION | PD_USEDEVMODECOPIES | PD_DISABLEPRINTTOFILE|PD_RETURNDC );
// Get the default printer.
dlg.DoModal();
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 ;
dc.StartPage();
dc.StartDoc(&docinfo);
//CGdiObject* pOldFont = dc.SelectStockObject(SYSTEM_FONT);
//dc.TextOut(50, 50, _T("Hello World!"), 12);
//dc.SelectObject(pOldFont);
TR_PRINT(_T("Orientation %d "),dm->dmOrientation);
dc.EndPage();
Replydc.EndDoc();
}
dc.Detach();
CDC::FromHandle(hdc)->DeleteDC();
::GlobalUnlock(dlg.m_pd.hDevMode);
How to open the page from the cusomizes app
Posted by Legacy on 11/12/2001 12:00amOriginally posted by: Ram
Hi
ReplyI 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 ?
thanks
ram
How to intercept the contents of Homepage before showing in the IE browser without proxy?
Posted by Legacy on 02/20/2001 12:00amOriginally 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 ^_^
email:ymkuo@neural.ee.ncku.edu.tw
by Kuo
Replymake a form print from a print button on page
Posted by Legacy on 08/08/2000 12:00amOriginally posted by: Alan Payne
ReplyExecWB and Debugger
Posted by Legacy on 10/05/1999 12:00amOriginally 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?
Reply
CWebBrowser2::GetDocument fails!
Posted by Legacy on 09/29/1999 12:00amOriginally 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.
Help!
Simon Hill
ReplyLoading, Please Wait ...