<%@ Language=vbscript%>
<%
Option Explicit
The value in the Dim strsubmit ' form to hold the Submit button
Value of saving network printer path in Dim strprinterpath ' form
Value of user name in Dim strusername ' form
The value of the password in Dim strpassword ' form
Dim strmessage ' form prints the value of the content
File system objects in Dim OBJFS ' VBScript
Network objects in Dim objwshnet ' WSH
Dim objprinter ' Print object
Strsubmit = Request.Form ("Submit")
%>
<HTML>
<HEAD>
<meta name= "generator" content= "Microsoft Visual Studio 6.0" >
</HEAD>
<BODY>
<%
If strsubmit = "" Then
%>
Note that:
Since this is a demo, where the account and password for NT are used unencrypted means in the ASP to pass the
The login process should be handled securely in the real application.
<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 printer path:</td>
<TD align=left nowrap><input type= "text" Id=printerpath Name=printerpath
Value= "< Domain >< Printer >" ></TD>
</TR>
<TR>
<TD align=right nowrap> Login account:</td>
<TD align=left nowrap><input type= "text" Id=username Name=username
value= "<% = strUserName%>" ></TD>
</TR>
<TR>
<TD align=right nowrap> Login Password:</td>
<TD align=left nowrap><input type= "password" Id=password
Name=password></td>
</TR>
<TR>
<TD align=right nowrap> Please enter the text you want to print:</td>
<TD align=left nowrap><textarea rows=2 cols=20 id=message
Name=message></textarea></td>
</TR>
<TR>
<TD align=right nowrap> </TD>
<TD align=left nowrap><input type= "Submit" value= "Submit"
Id=submit name=submit></td>
</TR>
</TABLE>
</FORM>
Once the above information has been submitted, it can be printed with the following code.
<%
Else
' Get the response information from the form.
Strprinterpath = Request.Form ("PrinterPath")
strUserName = Request.Form ("username")
strpassword = Request.Form ("password")
strmessage = Request.Form ("message")
We'll now use the VBScript FileSystemObject object and the WSH network object. The network object would
Give us the methods we need to open a printer connection, and the FileSystemObject'll allow us to stream our
Output to the printer. We create these objects in the following code example:
Set objFS = CreateObject ("Scripting.FileSystemObject")
Set objwshnet = CreateObject ("Wscript.Network")
' Connect a network printer using WSH
Objwshnet.addprinterconnection "LPT1", Strprinterpath, False, strUserName, strpassword
' Use a file system object to use a print device as a file
Set objprinter = Objfs.createtextfile ("LPT1:", True)
' Send text to a print device
Objprinter.write (strmessage)
' Turn off print device objects and make error trap handling
On Error Resume Next
Objprinter.close
' If an error occurs, turn off the print connection and output the error message
If ERR Then
Response.Write ("Error #" & CStr (Err.Number) & "" & Err.Description)
Err.Clear
Else
' Operation succeeded, output confirmation information
Response.Write ("<CENTER>")
Response.Write ("<table width=100% align=center border=0 cellspacing=1 cellpadding=1>")
Response.Write ("<TR><TD align=right><b> print message sent:</b></td>")
Response.Write ("<td align=left>" & strmessage & "</TD></TR>")
Response.Write ("<TR><TD align=right><b> network printer path:</b></td>")
Response.Write ("<td align=left>" & Strprinterpath & "</TD></TR>")
Response.Write ("<tr><td align=right><b> login account:</b></td>")
Response.Write ("<td align=left>" & strUserName & "</TD></TR>")
Response.Write ("</TABLE>")
Response.Write ("</CENTER>")
End If
' Cancel print connection
Objwshnet.removeprinterconnection "LPT1:"
Set objwshnet = Nothing
Set OBJFS = Nothing
Set Objprinter = Nothing
End If
%>
</BODY>
</HTML>