Server-Side Printing to a Networked Printer from ASP

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.

Close    To Top
  • Prev Article-Web Design:
  • Next Article-Web Design:
  • Now: Tutorial for Web and Software Design > Web Design > ASP > Web Design Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction