How Do I Print From Internet Explorer Without Raising a Confirmation Dialog?


NOTE: the concepts presented in this article are now considered obsolete possibly because better alternatives are available.

Update!! 11-29-2005
There seems to be a problem with the code presented in this article, so don't rely on it work anymore. When you click the print button you get an error and nothing gets printed.

So far, I haven't been able to isolate the problem or come up with a solution. It looks like this problem is caused by some security mechanism in Windows XP Service Pack 2. I haven't tested various versions of IE and Windows XP yet, but I am sure it doesn't run on IE 6, SP2 with Windows XP SP2 (well, it doesn't run on *my* IE 6, SP2 with Windows XP SP2 ;-) )


In some scenario's, it could be useful if you were able to print a page from within the browser, without the Print dialog popping up, and without the user confirming the print operation.
It is possible to have your page printed automatically, without the dialog. It's a bit tricky, it works on IE only and you may need to lower your security settings, but it does work. Copy and paste the following code to a new HTML document, run it in your browser and hit the print button. The page gets printed on the default printer without a confirmation dialog.
<html>
<head>
  <script language="JavaScript" type="text/javascript">
    DA = (document.all) ? 1 : 0
    function HandleError()
    {
      alert('\nNothing was printed. \n\nIf you do want to print ' +
          'this page, then\n click on the printer icon in the ' +
          'toolbar above.')
      return true;
    }
  </script>
</head>
<body>
  <form>
    <input type="button" value="Print Page" onClick="window.print();">
  </form>
  <script language="VBScript">
    Sub window_onunload()
      On Error Resume Next
      Set WB = nothing
      On Error Goto 0
    End Sub
    Sub Print()
      OLECMDID_PRINT = 6
      OLECMDEXECOPT_DONTPROMPTUSER = 2
      OLECMDEXECOPT_PROMPTUSER = 1

      On Error Resume Next

      If DA Then
        call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
      Else
        call WB.IOleCommandTarget.Exec _
            (OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","","")
      End If

      If Err.Number <> 0 Then
        If DA Then
          Alert("Nothing Printed :" & err.number & " : " & err.description)
        Else
          HandleError()
        End if
      End If
      On Error Goto 0
    End Sub
    If DA Then
      wbvers="8856F961-340A-11D0-A96B-00C04FD705A2"
    Else
      wbvers="EAB22AC3-30C1-11CF-A7EB-0000C05BAE0B"
    End If
    document.write("<object ID=""WB"" width=0 height=0 classid="" clsid:")
    document.write(wbvers & """> </object>")
  </script>
</body>
</html>

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Thursday, December 08, 2005 1:18:34 PM Malarvizhi said:
I need to print set of documents when i click a button, i used the web browser object for this,this works fine only for a single document, if i give more number of documents to print,all the documents not send to printer only few documents r going, plz give me some suggesstion

On Wednesday, February 07, 2007 8:54:06 AM varathan anantharaj said:
hello sir,

the above very much useful to me...
then i want one help to u.
how to set landscape in printer with out any dialog box....

i wait for ur reply....

On Wednesday, February 07, 2007 6:51:40 PM Imar Spaanjaars said:
Hi varathan,

AFAIK, you can't change that with script alone.

Maybe this trick works for you?

http://edacio.us/forum/comments.php?DiscussionID=39&page=1

Although most browsers won't allow you to do this....

Imar
On Wednesday, September 12, 2007 9:45:10 AM ravi said:
It's not working for me windows xp sp2
IE 6.0
error code is 438
please help if any other way is possible.
On Wednesday, September 12, 2007 10:19:23 AM Imar Spaanjaars said:
Hi ravi,

Pleae read the Update section at the top of this article.

Cheers,

Imar
On Tuesday, November 20, 2007 11:41:32 AM Tiago said:
This code works great in one of my 2 computers! with xp sp2 and ie 6 :)

The other one this code doesn't work (win 200 srv sp4 ie6) :( I get the folowing error: "438 object doesn't support this property or method"
On Tuesday, November 20, 2007 12:53:22 PM Imar Spaanjaars said:
Hi Tiago,

Did you read the introduction of this article? It's a known issue...

Imar
On Tuesday, July 22, 2008 4:18:52 AM Neil said:
Regarding "How Do I Print From Internet Explorer Without Raising a Confirmation Dialog"

...you now say "better alternatives are available". Could you tell me what they are and where I could find more about using them?

Thanks.
On Tuesday, July 22, 2008 7:02:35 AM Imar Spaanjaars said:
Hi Neil,

You only quoted a part of the message (which is the standard "obsolete message" that appears when I mark an item as obsolete in my CMS). The full message is:

"are now considered obsolete possibly because better alternatives are available."

The word possibly is the key here. Right below that it says this:

Update!! 11-29-2005
There seems to be a problem with the code presented in this article, so don't rely on it work anymore. When you click the print button you get an error and nothing gets printed.

So, in this case, there are no work arounds; AFAIK, this is no longer supported in IE.

Sorry.

Imar
On Wednesday, April 29, 2009 2:24:40 PM Garcia said:
hello all,

Is there a way to print with our the dialog box, or only can it be done witha activex object?

I have managed to doit for IE 6, IE7 but for firefox and other browsers estill send the dialog box.
On Wednesday, April 29, 2009 7:01:23 PM Imar Spaanjaars said:
Hi Garcia,

As indicated at the top of the article, this code no longer works; at least not in modern browsers. I know of no way around it, but that doesn't mean it doesn't exist.

Cheers,

Imar
On Wednesday, April 29, 2009 7:46:51 PM Garcia said:
Hi Imar,

Yes i read the hole article, and know that code is not working any more, i was asking if someone has yet find a solution to this issue. because it would be very helpfull to achive this for some enviroments. Intranets, etc.

if you can gime some pointers where i can look to do this?

thx for the quick anwerd back there.

regards
garcia
On Wednesday, April 29, 2009 7:50:56 PM Imar Spaanjaars said:
>> I know of no way around it

> if you can gime some pointers

I'd give them to you if I knew them.... My only suggestion is Google.... (or Live.com of course)

Cheers,

Imar
On Tuesday, May 26, 2009 11:33:41 AM Diseirra said:
Hi

I used this script, worked like a charm, but security issues came up when i hosted my application on the server. Can I convert this script into some active x control and sign that control ..??? I f yes then tell me how...
Please help, am half way done, but if the security issues stay i will have to look for another solution.
On Tuesday, May 26, 2009 6:52:13 PM Imar Spaanjaars said:
Hi Diseirra,

I have no idea; sorry.

Cheers,

Imar
On Tuesday, July 07, 2009 4:54:41 PM zeeshan malik said:
it is not working with IE7 or IE8.

Garcia: how did you managed to make it work with IE7?

Please help it is urgent.

regards
On Tuesday, July 07, 2009 8:04:52 PM Imar Spaanjaars said:
Hi zeeshan,

Please read the introduction and the comments of this article again; this code doesn't work at all anymore....

Imar
On Sunday, September 20, 2009 3:34:57 PM Ganesh Katore said:
Hi Imar,

I need to Print Html page directly on specific printer ( Non- Default Printer ) with Paper size (H-W) and page orientation.

Please help me

Thanks & Regards

Ganesh
On Sunday, September 20, 2009 3:38:54 PM Imar Spaanjaars said:
Hi Ganesh,

I know of no way to do this. As indicated at the start of the article, this code is now obsolete and no longer works.

Cheers,

Imar

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.