Solved

Automatic Printing from ASP

Posted on 2004-03-24
12
1,001 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
document.getElementById not worj with 2 IDs with the same name 10 64
Load data upon clicking a button 8 54
Filktering Alphabetically 8 46
Add Rows on a Table 8 55
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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/…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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