Printing Web Pages like Internet Explorer | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 7, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.