Solved

Automatic Printing from ASP

Posted on 2004-03-24
12
988 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

17 Experts available now in Live!

Get 1:1 Help Now