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

More by Author

Must Read