我设置了一个VBScript hta让我的用户选择默认打印机。我现在正试着让它尽可能清晰。
如果可能的话,我想从列出的每个网络打印机的前面隐藏打印服务器地址。
。对话框目前为每个打印机显示"\SERVPRT2Printer_Name",但我希望它只显示"Printer_Name"。或者,如果这是不可能的,那么也许只是格式化多余的文字与更微妙的颜色。
我在下面列出了我的代码——有人知道这是可能的吗?多谢
<html>
<head>
<title>Choose your default printer</title>
<style type="text/css">
body {
font-family:Verdana;
font-size: 12px;
color: #49403B;
background: #FFFFFF;
text-align: center;
}
</style>
<SCRIPT Language="VBScript">
Sub Window_Onload
window.resizeTo 500,550
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
strPrinter = objPrinter.Name
Set objOption = Document.createElement("OPTION")
objOption.Text = strprinter
objOption.Value = strPrinter
AvailablePrinters.Add(objOption)
Next
End Sub
Sub SetDefault
strPrinter = AvailablePrinters.Value
Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.SetDefaultPrinter strPrinter
Msgbox strprinter & " has been set as your default printer."
End Sub
</SCRIPT>
Select your default printer, then click the button below<p>
<select size="20" name="AvailablePrinters"></select><p>
<input type="button" value="Set as Default" onClick="SetDefault">
<p>
Close this window after setting a printer as default<p>
为此,您只需从显示的名称(Text
属性)中删除printserver。正则表达式替换可能是实现这一目标的最佳方法:
Set re = New RegExp
re.Pattern = "^\\.*?\"
For Each objPrinter in colPrinters
strPrinter = objPrinter.Name
Set objOption = Document.createElement("OPTION")
objOption.Text = re.Replace(strprinter, "")
objOption.Value = strPrinter
AvailablePrinters.Add(objOption)
Next
注意,当不同的打印服务器上有相同名称的打印机队列时,这可能会给用户造成混淆。
另外,我想为你的代码推荐一些可用性改进:
预先选择默认打印机,以便用户看到当前默认打印机:
For Each objPrinter in colPrinters strPrinter = objPrinter.Name Set objOption = Document.createElement("OPTION") objOption.Text = re.Replace(strprinter, "") objOption.Value = strPrinter If objPrinter.Default Then objOption.Selected = True AvailablePrinters.Add(objOption) Next
当选择改变时自动更改默认打印机,并移除Set as default 按钮。这就减少了用户的点击次数。
<select size="20" name="AvailablePrinters" onChange="SetDefault"></select>