Search code examples
javascriptinternet-explorerinternet-explorer-9oleprint-preview

Object doesn't support property or method 'ExecWB' when trying to call a print preview dialog in IE9


I am writing a web application for IE9 and I need to open the Print Preview dialog (not the window.print(), but the one where they can set the margins etc.)

I found this code in this question - Open another page or image in print Preview:

function PrintPreview()
{
  var OLECMDID =  7;

  /* OLECMDID values:
  * 6 - print
  * 7 - print preview
  * 0 - open window
  * 4 - Save As
  */

  var PROMPT = 1; // 1 PROMPT USER 2 DON'T PROMPT USER
  var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';


  window.document.body.insertAdjacentHTML('beforeEnd', WebBrowser);

  WebBrowser1.ExecWB(OLECMDID, PROMPT);
  WebBrowser1.outerHTML = "";

} 

However, this line fails:

WebBrowser1.ExecWB(OLECMDID, PROMPT);

The error message in both IE9 and IE11 is this:

"Object doesn't support property or method 'ExecWB'"

Update: more testing showed that the code works in some IE9/IE11 browsers (such as on my machine) but doesn't work on the browser inside an Azure VM and on my customer's configuration.

WebBrowser1 is not null and has a lot of properties and methods, such as the correct classid, but doesn't have the ExecWB() method.


Solution

  • Turns out that the code doesn't work if this option is set to "Disabled" in IE Tools => Internet Options => Security => Security Settings:

    Allow Scriptlets option in IE