ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash


 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
ASP and Personal Web Server
Show All Tips >>


Server-Side Printing to a Networked Printer from ASP

by John Peterson

A while back I wrote Server-Side Printing from ASP which illustrated printing to a printer directly connected to your web server via an ASP script. The only reason I wrote that article was because Microsoft removed the only resource (that I knew of) that told people how to do it. That resource, an article entitled "Using ASP and WSH to Print on Your Intranet", also covered printing to a network printer.

Since writing my article on printing from ASP, I've recieved quite a few requests for the code to print to a network printer, but until now have been unable to provide it. I've had no luck recreating it (mainly because I don't have a networked printer to test on) and I was having an even harder time finding a copy of the original article.

Until now. I won't go into how I found the article, but let's just say that it's a good thing I'm a pack rat!

The Code

Before I give you the code... here are a few warnings. I did not write this code. I've never used it and am currently unable to test it due to the previously mentioned lack of a networked printer. As far as I know it should work fine, but if it doesn't there's not much I can do about it. Aside from the code listing below... you're on your own.


<%@ Language=VBScript %>
<%
Option Explicit

Dim strSubmit         'Form value for the Submit Button
Dim strPrinterPath    'Form value for Network Path to Printer
Dim strUsername       'Form value for Username
Dim strPassword       'Form value for Password
Dim strMessage        'Form value for Message to Print
Dim objFS             'VBScript File System Object
Dim objWSHNet         'Windows Script Host Network Object
Dim objPrinter        'Printer Object to stream text to

strSubmit = Request.Form("Submit")
%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>

<%
'
' If we have not received the results from the form we must
' display it.
'
If strSubmit = "" Then
%>

<FORM action="ASPPrint.asp" method=POST id=form name=form>
   <TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=1 CELLPADDING=1>
      <TR>
         <TD ALIGN=right NOWRAP>Network Path to the Printer:</TD>
         <TD ALIGN=left NOWRAP><INPUT type="text" id=printerpath
            name=printerpath
            value="\\< Domain >\< Printer >"></TD>
      </TR>
      <TR>
         <TD ALIGN=right NOWRAP>Login ID:</TD>
         <TD ALIGN=left NOWRAP><INPUT type="text" id=username
            name=username
            value="<% = strUsername %>"></TD>
      </TR>
      <TR>
         <TD ALIGN=right NOWRAP>Password:</TD>
         <TD ALIGN=left NOWRAP><INPUT type="password" id=password
            name=password></TD>
      </TR>
      <TR>
         <TD ALIGN=right NOWRAP>Message to print:</TD>
         <TD ALIGN=left NOWRAP><TEXTAREA rows=2 cols=20 id=message
            name=message></TEXTAREA></TD>
      </TR>
      <TR>
         <TD ALIGN=right NOWRAP>&nbsp;</TD>
         <TD ALIGN=left NOWRAP><INPUT type="submit" value="Submit"
            id=submit name=submit></TD>
      </TR>
   </TABLE>
</FORM>

<%
Else
   '
   ' Get information from our form
   '
   strPrinterPath = Request.Form("printerpath")
   strUsername = Request.Form("username")
   strPassword = Request.Form("password")
   strMessage = Request.Form("message")

   '
   ' Create FileSystem Object and Windows Script Host Network Object
   '
   Set objFS = CreateObject("Scripting.FileSystemObject")
   Set objWSHNet = CreateObject("WScript.Network")

   '
   ' Connect to Network Printer from Windows Script Host
   '
   objWSHNet.AddPrinterConnection "LPT1", strPrinterPath, False,
      strUsername, strPassword

   '
   ' Open Print device as a file using the File System Object
   '
   Set objPrinter = objFS.CreateTextFile("LPT1:", True)

   '
   ' Send text to print device using the File System Object
   '
   objPrinter.Write(strMessage)

   '
   ' Close the print device object and trap for errors
   '

   On Error Resume Next
   objPrinter.Close

   '
   ' If an error has occurred while closing the printer connection,
   ' output what went wrong.
   '
   If Err Then
      Response.Write ("Error # " & CStr(Err.Number) & " " & Err.Description)
      Err.Clear
   Else
      '
      ' The operation succeeded.  Output a confirmation
      '
      Response.Write("<CENTER>")
      Response.Write("<TABLE WIDTH=100% ALIGN=center BORDER=0>")
      Response.Write("<TR><TD ALIGN=RIGHT><B>Message Sent:</B></TD>")
      Response.Write("<TD ALIGN=LEFT>" & strMessage & "</TD></TR>")
      Response.Write("<TR><TD ALIGN=RIGHT><B>Path to Network Printer:")
      Response.Write("</B></TD>")
      Response.Write("<TD ALIGN=LEFT>" & strPrinterPath & "</TD></TR>")
      Response.Write("<TR><TD ALIGN=RIGHT><B>Login ID:</B></TD>")
      Response.Write("<TD ALIGN=LEFT>" & strUsername & "</TD></TR>")
      Response.Write("</TABLE>")
      Response.Write("</CENTER>")
   End If

   '
   ' Remove the printer connection
   '
   objWSHNet.RemovePrinterConnection "LPT1:"
   Set objWSHNet  = Nothing
   Set objFS      = Nothing
   Set objPrinter = Nothing
End If
%>
</BODY>
</HTML>

The above code was written by Jeff Sandquist from Microsoft and originally published in an article called "Using ASP and WSH to Print on Your Intranet" in the old "Servin' It Up" on MSDN. If anyone knows of a copy of the article that is still available online please let me know and I'll be happy to link to it.

Update: The Original Article... Sort Of...

One of our visitors sent in the following link where you can find the text of the original article. The formatting leaves something to be desired, but I guess it's better then nothing.

Scratch that... apparently the link we were originally sent was one that somehow bypassed the site's style sheets... here's a better looking version: http://www.taltech.com/support/bcax/ASP_Printing.html.

Download

You can download a zip file containing the script above from here: aspnetprint.zip (1.3 KB) Once again... I haven't tested this code and am not even sure it will work, but it's worth a shot.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES