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
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!
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 %>
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>
<TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=1 CELLPADDING=1>
<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"
' 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,
' 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>")
' Remove the printer connection
Set objWSHNet = Nothing
Set objFS = Nothing
Set objPrinter = Nothing
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.