VBS using LIKE to compare strings “Sub or Function not defined”

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I'm trying to make a script to connect a network printer to a user computer. The script uses the computer name who needs the printer as a parameter.

Printers names are similar their printserver name, eg. server_USA has printers like printer_USA01, printer_USA02.

But it's throwing the error "Sub or Function not defined" when arrives at the first like... why ?

Set shl = WScript.CreateObject("WScript.Shell") strName = Wscript.Arguments.Item(0)  'input Printer name strPrinter = InputBox("Please enter share name of printer to install:", _     "Add network printer")  if strPrinter = "" then     msgbox "Can't be empty."     WScript.quit  elseif strPrinter Like "printer_USA*" then     strServer = server_USA  elseif strPrinter Like "printer_SPAIN*" then     strServer = server_SPAIN  else     'Printer name NOT registered, input printserver manually:     strServer = inputbox("Please enter the name of the printserver","printserver")      if strServer = "" then         msgbox "Can't be empty."         WScript.quit     End if  End if  'ADD shl.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\" & strName & " /n\\" & strServer & "\" & strPrinter 

回答1:

there is no Like operator in VBScript. You could use Instr.

if strPrinter = "" then     msgbox "Can't be empty."     WScript.quit  elseif Instr( 1, strPrinter, "printer_USA", vbTextCompare ) > 0 then     strServer = server_USA 

The vbTextCompare constant ( value=1) is used to Perform a textual comparison



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!