Printing directly from Internet Explorer without print dialog

Up until today I always thought that it was impossible to print directly (or silently) from a web page in Internet Explorer. The argument of losing control as to what you want to print seemed plausible. But I found out by Googling that it is possible. Here’s how:

There is an ActiveX control with CLSID {8856F961-340A-11D0-A96B-00C04FD705A2} or PROGID Shell.Explorer.2 that allows you to execute advanced and powerful commands to the page in which it is embedded. The key to this is the IWebBrowser2::ExecWB method, that accepts amongst others an OLE command. For printing we need the IDM_Print (value is 6) command, but there are a zillion others. More on these MSHTML command identifiers can be found here (or here if you have MSDN library installed).

The piece of JavaScript that does the printing silently is below. You can hook it to your page’s onload event of the element.

function DoPrint()
  var WebBrowser = ”;
  document.body.insertAdjacentHTML(‘beforeEnd’, WebBrowser); 
  ctlWebBrowser.ExecWB(6, -1);
  ctlWebBrowser.outerHTML = “”;

The ActiveX control is available on every machine that has IE4.0 or higher installed. The ExecWB has a second parameter of -1 to not show the normal print dialog that window.print() will normally show. A value of 2 should have the same result, whereas 1 should show the dialog.

You can use this piece of code in combination with frames as well. If you create a frameset of two frames with 100% and 0% in height, you can target the invisible frame, load the page to print and inject the WebBrowser control as shown above.

Some more useful links here, here and here.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s