<%@ Language=vbscript %>
<%
Option Explicit
Dim strSubmit 'The value used to save the submit button in Form
Dim strPrinterPath 'The value of the network printer path saved in the Form
Dim strUsername 'The value of the username in the Form
Dim strPassword 'Value of password in Form
Dim strMessage 'Form print content value
Dim objFS 'File system object in VBScript
Dim objWS.NET 'Network objects in 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 demonstration, the account number and password related to NT are transmitted in ASP using unencrypted means.
In real applications, the login process should be handled securely.
<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>
After the above information is submitted, you can print according to the following code.
<%
Else
' Get response information from form.
strPrinterPath = Request.Form(printerpath)
strUsername = Request.Form(username)
strPassword = Request.Form(password)
strMessage = Request.Form(message)
We will now use the VBScript FileSystemobject object and the WSH Network object. The Network object will
give us the methods we need to open a printer connection, and the FileSystemObject will 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)
' Use WSH to connect to a network printer
objWSHNet.AddPrinterConnection LPT1, strPrinterPath, False, strUsername, strPassword
' Use the file system object to use the printing device as a file
Set objPrinter = objFS.CreateTextFile(LPT1:, True)
' Send text to the printing device
objPrinter.Write(strMessage)
'Close the printing device object and perform error trap processing
On Error Resume Next
objPrinter.Close
' If an error occurs, close the print connection and output the error message
If Err Then
Response.Write (Error # & CStr(Err.Number) & & Err.Description)
Err.Clear
Else
' The operation is successful and a confirmation message is output.
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 the print connection
objWSHNet.RemovePrinterConnection LPT1:
Set objWSHNet = Nothing
Set objFS = Nothing
Set objPrinter = Nothing
End If
%>
</BODY>
</HTML>