Solved

Automatic Printing from ASP

Posted on 2004-03-24
12
995 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
ID: 10668812
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
ID: 10668818
BTW, it only works in IE and please note I have not tried it out.

-David251
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10668927
@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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

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

Expert Comment

by:david251
ID: 10669069
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
ID: 10669489
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
 
LVL 31

Expert Comment

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

Author Comment

by:BetsyV
ID: 10670430
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
ID: 10670503
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
ID: 10670840
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
ID: 10670851
no problem, glad I can help
-David251
0
 

Expert Comment

by:kwear
ID: 10836064
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help editing script 3 84
document.getElementById not worj with 2 IDs with the same name 10 57
Debugging Html 8 31
Validating Date 4 28
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…

685 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