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

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @
What is and Why Do I Need It?

Upgrade to the 5.5 Scripting Engine
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

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")
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
' 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>
         <TD ALIGN=right NOWRAP>Network Path to the Printer:</TD>
         <TD ALIGN=left NOWRAP><INPUT type="text" id=printerpath
            value="\\< Domain >\< Printer >"></TD>
         <TD ALIGN=right NOWRAP>Login ID:</TD>
         <TD ALIGN=left NOWRAP><INPUT type="text" id=username
            value="<% = strUsername %>"></TD>
         <TD ALIGN=right NOWRAP>Password:</TD>
         <TD ALIGN=left NOWRAP><INPUT type="password" id=password
         <TD ALIGN=right NOWRAP>Message to print:</TD>
         <TD ALIGN=left NOWRAP><TEXTAREA rows=2 cols=20 id=message
         <TD ALIGN=right NOWRAP> </TD>
         <TD ALIGN=left NOWRAP><INPUT type="submit" value="Submit"
            id=submit name=submit></TD>
   ' 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
   ' Close the print device object and trap for errors
   On Error Resume Next
   ' 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)
      ' The operation succeeded.  Output a confirmation
      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("<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>")
   End If
   ' Remove the printer connection
   objWSHNet.RemovePrinterConnection "LPT1:"
   Set objWSHNet  = Nothing
   Set objFS      = Nothing
   Set objPrinter = Nothing
End If

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:


You can download a zip file containing the script above from here: (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
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers