Link to home
Start Free TrialLog in
Avatar of BetsyV
BetsyV

asked on

Automatic Printing from ASP

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 !!!!
Avatar of david251
david251

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
BTW, it only works in IE and please note I have not tried it out.

-David251
@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!
All that code does is call the Dialog Box.
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

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

-1, hmmmm.... I tried 1, 2, and 3, but not negatives!  Probably works.
Avatar of BetsyV

ASKER

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 !
ASKER CERTIFIED SOLUTION
Avatar of david251
david251

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BetsyV

ASKER

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 !!!
no problem, glad I can help
-David251
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?