我已经设置了VBScript hta,让我的用户选择默认打印机。我目前正试图在视觉上尽可能清晰。
我们的打印机列出类似于:
4th floor
1st floor
Block 2
3rd floor
etc.
是否有人知道我可以对代码进行更改以按名称对这些输出进行排序?非常感谢
<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 & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
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
If objPrinter.Default Then objOption.Selected = True
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>
Click a printer name to set it as your default<p>
<select size="20" name="AvailablePrinters" onChange="SetDefault"></select>
<p>
Close this window when done
答案 0 :(得分:0)
您可以将打印机名称添加到数组中,然后使用简单的冒泡排序。唯一棘手的部分是您还获得默认打印机。因此,您必须在迭代Win32_Printer
集合时保存它。
' Create an array to hold the printer names...
ReDim a(colPrinters.count - 1)
' Add each printer name to the array. Also, save the name of the default printer.
Dim i, strDefault
For Each objPrinter In colPrinters
a(i) = objPrinter.Name
If objPrinter.Default Then strDefault = objPrinter.Name
i = i + 1
Next
' Sort the array...
Sort a
For i = 0 To UBound(a)
' Create your <option>...
If a(i) = strDefault Then objOption.Selected = True
Next
' Basic bubble sort...
Sub Sort(a)
Dim i, j, temp
For i = UBound(a) - 1 To 0 Step -1
For j = 0 To i
If a(j) > a(j + 1) Then
temp = a(j + 1)
a(j + 1) = a(j)
a(j) = temp
End If
Next
Next
End Sub
答案 1 :(得分:0)
这是一个从stdin读取的代码片段,添加到断开连接的记录集,对记录集进行排序,读取和写入stdout。
With rs
.Fields.Append "SortKey", 201, 260
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, Arg(3), Arg(4) - Arg(3))
.AddNew
.Fields("SortKey").value = SortKey
.Fields("Txt").value = Lne
.UpDate
Loop
If Arg(2) = "a" then SortColumn = "SortKey ASC"
If Arg(2) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
或者您可以使用.net排序数组/集合。
Set sortthing = CreateObject("System.Collections.SortedList")
请参阅.NET framework docs中的SortedList类。
答案 2 :(得分:0)
我使用ArrayList
和Dictionary
,如下所示:
Set printerNames = CreateObject("System.Collections.ArrayList")
Set printers = CreateObject("Scripting.Dictionary")
For Each objPrinter in colPrinters
name = re.Replace(objPrinter.Name, "")
printerNames.Add name
printers.Add name, objPrinter
Next
printerNames.Sort
For Each name in printerNames
Set objOption = Document.createElement("OPTION")
objOption.Text = name
objOption.Value = printers(name).Name
If printers(name).Default Then objOption.Selected = True
AvailablePrinters.Add(objOption)
Next