Using ASP to print function of network

Source: Internet
Author: User
Tags file system print object visual studio
<%@ 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>
&LT;TD align=right nowrap> Network printer path:</td>
&LT;TD align=left nowrap><input type= "text" Id=printerpath Name=printerpath
Value= "\\< Domain >\< Printer >" ></TD>
</TR>
<TR>
&LT;TD align=right nowrap> Login account:</td>
&LT;TD align=left nowrap><input type= "text" Id=username Name=username
value= "<% = strUserName%>" ></TD>
</TR>
<TR>
&LT;TD align=right nowrap> Login Password:</td>
&LT;TD align=left nowrap><input type= "password" Id=password
Name=password></td>
</TR>
<TR>
&LT;TD align=right nowrap> Please enter the text you want to print:</td>
&LT;TD align=left nowrap><textarea rows=2 cols=20 id=message
Name=message></textarea></td>
</TR>
<TR>
&LT;TD align=right nowrap> </TD>
&LT;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 ("&LT;TR&GT;&LT;TD align=right><b> print message sent:</b></td>")
Response.Write ("<td align=left>" & strmessage & "</TD></TR>")
Response.Write ("&LT;TR&GT;&LT;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> turn from: Dynamic Network production guide www.knowsky.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.