Solved

Automatic Printing from ASP

Posted on 2004-03-24
12
987 Views
Last Modified: 2012-06-27
Is there any way to print a page automatically to a user's default printer, without a print dialog box coming up?  I have a request  from one of my users to submit a form, the print the results page automatically, so no button to click, no link to click, just when the page is displayed it prints.

I don't have to worry about different versions of IE because this is on our intranet, and I can control it.  Everyone here is IE version 5.5 or greater.  Right now, I'm using window.print(), but my contact who has requested the application doesn't like the print dialog box (user has the ability to cancel the print if it displays).

Any suggestions are always appreciated !!!!
0
Comment
Question by:BetsyV
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 19

Expert Comment

by:david251
Comment Utility
BetsyV,

I found this on microsofts site (click Figure1 on http://msdn.microsoft.com/msdnmag/issues/01/03/web/default.aspx )

I hope it helps.

<html>
<head>
<script language="VBScript">
SUB Print()
    OLECMDID_PRINT = 6
    OLECMDEXECOPT_DONTPROMPTUSER = 2
    OLECMDEXECOPT_PROMPTUSER = 1
    'Bring focus to the document so that it will be what prints:
    document.body.focus()
    'Call the "Print" command. Since the Internet Explorer/DOM didn't add
    'the "print()" method until 5.0
    on error resume next
    call IEWB.ExecWB (OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER)
    if err.number <> 0 then
        alert "Nothing Printed"
    end if
END SUB
</script>
</head>
<body>
<object id="IEWB" width=0 height=0 classid="clsid:8856F961-340A-11D0-
                                     A96B-00C04FD705A2"></object>

Good Luck,

-David251
0
 
LVL 19

Expert Comment

by:david251
Comment Utility
BTW, it only works in IE and please note I have not tried it out.

-David251
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
@david251 - I dont' think that works.  And, I have yet to see a way to bypass the Print Dialog box because it's browser dependent....but haven't looked very hard!
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
All that code does is call the Dialog Box.
0
 
LVL 19

Expert Comment

by:david251
Comment Utility
sorry, it is supposed to work.

I just tried and you are right.

Sorry it didn't work, you just can't trust the guys from microsoft.

-David251

0
 
LVL 19

Expert Comment

by:david251
Comment Utility
I figured it out !!!  

just change

call IEWB.ExecWB (OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER)

to
call IEWB.ExecWB (OLECMDID_PRINT, -1)


Here is the source:
http://www.faqts.com/knowledge_base/view.phtml/aid/9169

Happy Printing!
-David251

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 31

Expert Comment

by:alorentz
Comment Utility
-1, hmmmm.... I tried 1, 2, and 3, but not negatives!  Probably works.
0
 

Author Comment

by:BetsyV
Comment Utility
I'm close, but have something incorrect.  I created a page (test.asp), which has the following in it:

<html>
<head>
<script language = "VBScript">
      Sub Print()
            OLECMDID_PRINT = 6
            OLECMDEXECOPT_DONTPROMPTUSER = 2
            OLECMDEXECOPT_PROMPTUSER = 1
            document.body.focus()
            on error resume next
            call IEWB.ExecWB (OLECMDID_PRINT, -1)
            if err.number <> 0 then
                  Alert "Nothing Printed"
            End if

      End Sub
</script>
</head>

<body>

<object id="IEWB" width=0 height=0 classid="classid:8856F961-340A-11D0-A96B-00C04FD705A2"></object>

<p>STUFF TO PRINT</p>

</body>
</html>

This looks right to me, but nothing happens.  Then I took out the Sub Print() and End Sub, just to see what happened.  When I did this, I get an error "object required document.body".

Last try, just for fun, I commented this line out (document.body.focus), and then it goes to the error messag (nothing printed).  I tried changing the Alert to show the error number, and get 424.

I really appreciate the help.  Do you see anything obvious that I have wrong?

THANKS !
0
 
LVL 19

Accepted Solution

by:
david251 earned 500 total points
Comment Utility
Here is the entire code.  It will print after the page has loaded.

<html>
<head>
<script language="VBScript">
SUB Print()
    OLECMDID_PRINT = 6
    OLECMDEXECOPT_DONTPROMPTUSER = 2
    OLECMDEXECOPT_PROMPTUSER = 1
    'Bring focus to the document so that it will be what prints:
    document.body.focus()
    'Call the "Print" command. Since the Internet Explorer/DOM didn't add
    'the "print()" method until 5.0
    on error resume next
    call IEWB.ExecWB (OLECMDID_PRINT, -1)
    if err.number <> 0 then
        alert "Nothing Printed"
    end if
END SUB
</script>
</head>
      <body onload="print()">
            <object id="IEWB" width="0" height="0" classid="clsid:8856F961-340A-11D0-A96B-00C04FD705A2">
            </object>
            this is a test
      </body>
</html>

-David251
0
 

Author Comment

by:BetsyV
Comment Utility
I had to change a security setting in IE, but once I did this, the above worked.  Thanks so much for all the help.  I really appreciate it !!!
0
 
LVL 19

Expert Comment

by:david251
Comment Utility
no problem, glad I can help
-David251
0
 

Expert Comment

by:kwear
Comment Utility
Hi, excellent post..taking your code and trying to add a twist to it.  Would like to make a form to load, have an input then excute the above script.  Tried modifying but having a little problem...can someone give a hand?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Classic ASP "Generic request error" 4 31
SP to delete duplicates 15 54
Error viewing ASP page 12 76
ASP Button to clear text 4 15
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now