在 VBScript 中使用 Split 超出范围的下标
Subscript out of range using Split in VBScript
我发现这个 VBScript 可以显示 HTA 应用程序中默认打印机的名称。只要有默认打印机设置,它就可以正常工作,但是当我 运行 它在新用户加载时(当绝对没有设置为默认打印机时)它会给出错误:
Subscript out of range: '[number:0]'
有什么办法可以解决这个问题吗?
<SCRIPT Language="VBScript">
dim defaultprn
Set WshShell = CreateObject("WScript.Shell")
defaultprn = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
arrPrnStr = Split(defaultprn,",")
fullPrinterString = arrPrnStr(0)
arrFullPrnStr = Split(fullPrinterString,"\")
shortPrinterName= arrFullPrnStr(Ubound(arrFullPrnStr))
document.write("Default printer: " + shortPrinterName)
If shortPrinterName="Fax" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Print to PDF" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Write" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Office Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2005" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2006" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2007" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2008" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2009" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2010" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2011" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2012" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2014" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2015" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2016" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2017" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2018" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2019" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2020" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2021" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2022" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2023" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2024" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2025" Then
changedefaultprinter()
ElseIf shortPrinterName="OneNote" Then
changedefaultprinter()
ElseIf shortPrinterName=Empty Then
changedefaultprinter()
ElseIf shortPrinterName=Null Then
changedefaultprinter()
End If
</script>
提前致谢。
问题是您正在尝试创建一个包含空变量的数组。由于用户没有默认打印机,注册表读取返回空白。将该空白放入数组会产生错误。所以你只要检查它不是空的,例如使用<>。
我添加了一个额外的 IF 语句,以便在它执行其余代码之前进行检查。底部还有一个 else,这意味着如果它是空的,则为没有默认打印机输出自定义消息。
<SCRIPT Language="VBScript">
dim defaultprn
Set WshShell = CreateObject("WScript.Shell")
defaultprn = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
if defaultprn <> "" Then
arrPrnStr = Split(defaultprn,",")
fullPrinterString = arrPrnStr(0)
arrFullPrnStr = Split(fullPrinterString,"\")
shortPrinterName= arrFullPrnStr(Ubound(arrFullPrnStr))
document.write("Default printer: " + shortPrinterName)
If shortPrinterName="Fax" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Print to PDF" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Write" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Office Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2005" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2006" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2007" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2008" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2009" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2010" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2011" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2012" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2014" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2015" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2016" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2017" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2018" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2019" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2020" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2021" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2022" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2023" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2024" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2025" Then
changedefaultprinter()
ElseIf shortPrinterName="OneNote" Then
changedefaultprinter()
ElseIf shortPrinterName=Empty Then
changedefaultprinter()
ElseIf shortPrinterName=Null Then
changedefaultprinter()
Else
document.write("Default printer: " + "None")
changedefaultprinter()
End if
End If
</script>
我发现这个 VBScript 可以显示 HTA 应用程序中默认打印机的名称。只要有默认打印机设置,它就可以正常工作,但是当我 运行 它在新用户加载时(当绝对没有设置为默认打印机时)它会给出错误:
Subscript out of range: '[number:0]'
有什么办法可以解决这个问题吗?
<SCRIPT Language="VBScript">
dim defaultprn
Set WshShell = CreateObject("WScript.Shell")
defaultprn = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
arrPrnStr = Split(defaultprn,",")
fullPrinterString = arrPrnStr(0)
arrFullPrnStr = Split(fullPrinterString,"\")
shortPrinterName= arrFullPrnStr(Ubound(arrFullPrnStr))
document.write("Default printer: " + shortPrinterName)
If shortPrinterName="Fax" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Print to PDF" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Write" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Office Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2005" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2006" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2007" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2008" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2009" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2010" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2011" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2012" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2014" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2015" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2016" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2017" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2018" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2019" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2020" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2021" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2022" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2023" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2024" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2025" Then
changedefaultprinter()
ElseIf shortPrinterName="OneNote" Then
changedefaultprinter()
ElseIf shortPrinterName=Empty Then
changedefaultprinter()
ElseIf shortPrinterName=Null Then
changedefaultprinter()
End If
</script>
提前致谢。
问题是您正在尝试创建一个包含空变量的数组。由于用户没有默认打印机,注册表读取返回空白。将该空白放入数组会产生错误。所以你只要检查它不是空的,例如使用<>。 我添加了一个额外的 IF 语句,以便在它执行其余代码之前进行检查。底部还有一个 else,这意味着如果它是空的,则为没有默认打印机输出自定义消息。
<SCRIPT Language="VBScript">
dim defaultprn
Set WshShell = CreateObject("WScript.Shell")
defaultprn = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
if defaultprn <> "" Then
arrPrnStr = Split(defaultprn,",")
fullPrinterString = arrPrnStr(0)
arrFullPrnStr = Split(fullPrinterString,"\")
shortPrinterName= arrFullPrnStr(Ubound(arrFullPrnStr))
document.write("Default printer: " + shortPrinterName)
If shortPrinterName="Fax" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Print to PDF" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft XPS Document Write" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Microsoft Office Document Image Writer" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2005" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2006" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2007" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2008" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2009" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2010" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2011" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2012" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2013" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2014" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2015" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2016" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2017" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2018" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2019" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2020" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2021" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2022" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2023" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2024" Then
changedefaultprinter()
ElseIf shortPrinterName="Send To OneNote 2025" Then
changedefaultprinter()
ElseIf shortPrinterName="OneNote" Then
changedefaultprinter()
ElseIf shortPrinterName=Empty Then
changedefaultprinter()
ElseIf shortPrinterName=Null Then
changedefaultprinter()
Else
document.write("Default printer: " + "None")
changedefaultprinter()
End if
End If
</script>