GoTo "Expected Statement" - vbscript

I am attempting to use the GoTo command (I do not know any alternatives and it works fine in batch). Whenever I attempt to load the program, I get this error:
Here is basically where the error is (line 11 column 3)
top:
input = InputBox("Enter normal text:", "Message Encrypt Style 2", "Text goes here")
If input = "" Then
Y = MsgBox("You inputed nothing!", vbRetryCancel+64, "Huh?")
If Y = 2 Then
WScript.Quit
Else
If Y = 4 Then
GoTo top
Else
If input = 2 Then
WScript.Quit

VBScript doesn't have a Goto statement, and there's a cleaner approach anyway.
Do
input = InputBox(...)
If IsEmpty(input) Or input = "2" Then
WScript.Quit
ElseIf input = "" Then
MsgBox "No input."
End If
Loop Until input <> ""

try this
Option Explicit
Dim Input ' as string
Dim Y ' as msgbox response
Input = ""
Do Until Input <> ""
Input= InputBox("Enter normal text:", "Message Encrypt Style 2", "Text goes here")
If Input = "" Then
Y = Msgbox ("You input nothing", vbRetryCancel, "Huh?")
If Y = vbCancel Then
WScript.Quit
End If
ElseIf Input = "2" Then
WScript.Quit
End If
Loop
' Proceed here if input is valid

Vbscript is a structured programming language and one of the main goals of structured programming is to eliminate the goto statement as it's considered harmful. Vbscript does have a goto for exceptions, but these are only meant for resource cleanup prior to a program exit.

Related

How can I read all arguments with the VBS-SAPI-VOICE?

I want to create a program, that automaticly reads all
start-arguments. This is what I end with after some hours
of researching and debugging but it still not working.
(I'm not very experienced in VBScript.)
The Error Message is something like "Instruction expected" or "statement expected"
I dont know whats the right translation.(I use the German Version. The original Errormessage is: "Anweisung erwartet")
Private Sub Say()
Set VOICE = createobject("sapi.spvoice")
Set Args = WScript.Arguments
Count = 0
While(count > Args.Count)
VOICE.speak(WScript.Arguments(count))
count = count + 1
End While
End Sub
Say()
Have someone an idea?
I hope you understood what I mean. (My English is awful)
Refer to this you can write something like that :
Set VOICE = createobject("sapi.spvoice")
' Store the arguments in a variable:
Set objArgs = Wscript.Arguments
If objArgs.Count = 0 Then
Wscript.Echo "Missing parameters"
VOICE.Speak "Missing parameters"
End If
' Display all command-line arguments
For Each strArg in objArgs
WScript.Echo strArg
VOICE.Speak strArg
Next

Vbscript hard validation

Hy I am new to programming vbscript.I am trying to make a textbox for user in which user has to enter file version(format 1,0,0,0).user must type one integer within commas.but I failed to make a validation script to do so.. Can you please help me. Thanks in advance.I have been created this script to perform other kind of validation but I don't know how to do the validation for x,y,z,h format..
Do
dtm = InputBox("Please Enter a Numeric File version using commas", _
"File version")
Select Case True
Case isNull(dtm), (not isNumeric(dtm)), dtm = "", dtm = empty, (dtm < 1 OR dtm > 9)
MsgBox "Please enter between 1 and 9"
Case else
Exit do
End Select
Loop While True
'script on test pass
Something like this should do the job (have not tested with wscript/cscript), assuming accepted numbers are 0 to 9.
Function GetVersionNumber() As String
Dim iParts, sVersion, oTmp, dtm
iParts = 0
sVersion = ""
Do
dtm = InputBox("Please Enter a Numeric File version using commas", "File version")
For Each oTmp In Split(dtm, ",")
If IsNumeric(Trim(oTmp)) Then
If Len(sVersion) > 0 Then sVersion = sVersion & ","
sVersion = sVersion & Trim(oTmp)
iParts = iParts + 1
Else
MsgBox "Please enter between 0 and 9!" & vbCrLf & "You have typed " & oTmp
iParts = 0 ' Reset to zero accepted parts
sVersion = "" ' Reset to zero length string
End If
Next
Loop Until iParts = 4 ' x,y,z,h format <- 4 parts
GetVersionNumber = sVersion
End Function

VBS using LIKE to compare strings "Sub or Function not defined"

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
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
you can use StrComp to have same result in this way
If StrComp(strPrinter,"printer_USA",vbTextCompare)=0 then
strServer = server_USA
End IF
equal 0 mean zero different between strPrinter and printer_USA with ignore the letter case because we use vbTextCompare .
You can replace vbTextCompare with 1 and you will have same result.
If letter case is important you can use vbBinaryCompare or 0.
A way to do that with select case. This version of instr() is case sensitive, but other versions aren't. instr() returns the position of the found substring, which here is always one.
select case 1
case instr(strPrinter, "") + 1
wscript.echo "empty"
case instr(strPrinter, "printer_USA")
wscript.echo "server_USA"
case instr(strPrinter, "printer_SPAIN")
wscript.echo "server_SPAIN"
case instr(strPrinter, "printer_ITALY"), instr(strPrinter, "printer_RUSSIA")
wscript.echo "other known ones"
case else
wscript.echo "not registered"
end select
I used the following alternative (VBScript Regular Expressions)…
Uses slightly different syntax from LIKE but easiest solution to make a match successfully similar to LIKE operator.
dim regExp
set regExp=CreateObject("VBScript.RegExp")
regExp.IgnoreCase = true
regExp.Global = true
regxp.Pattern = ".*Test Pattern.*" ' example only, basic pattern
if regExp.Test(MyString) then
' match successful
end if

VbScript make computer say something error

I have this file that makes a computer say something. I want it to loop with a VbCancel function. I get this error. Code so far:
Do
Dim Message, Speak
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Speak)
Speak.Speak Message
If Len(Speak) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Sub
End If
Loop
BTW the error is Invalid exit statement
I'm working on Windows 7
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop
You had several issues here
Exit Sub is for subroutines. You were trying to exit a Do loop
Speak is an object. I dont know if it has a string property but it is not itself a string. Both Len(Speak) and "You entered: " & Speak has Speak changed to Message.
You were missing an End If
I moved the Dim statements out of the loop. No point recreating the object over and over again.
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop

exe with accepting runtime parameter

how o write vb code which can except parameter at runtime
ex. My exe is "readfile.exe" and if i want to give file name rom command line the command to be executed will be
readfile.exe filename
it should take the file name parameter and perform the action
Look at the Command function, that should give you all the parameters that were passed in.
I can't find the VB6 docs for it online, but MSDN have the docs for the VBA version, and that's usually the same so I'd suggest looking here for more info. And it even has a full sample here.
You can do something like this:
Sub Main()
Dim a_strArgs() As String
Dim blnDebug As Boolean
Dim strFilename As String
Dim i As Integer
a_strArgs = Split(Command$, " ")
For i = LBound(a_strArgs) To UBound(a_strArgs)
Select Case LCase(a_strArgs(i))
Case "-d", "/d"
' debug mode
blnDebug = True
Case "-f", "/f"
' filename specified
If i = UBound(a_strArgs) Then
MsgBox "Filename not specified."
Else
i = i + 1
End If
If Left(a_strArgs(i), 1) = "-" Or Left(a_strArgs(i), 1) = "/" Then
MsgBox "Invalid filename."
Else
strFilename = a_strArgs(i)
End If
Case Else
MsgBox "Invalid argument: " & a_strArgs(i)
End Select
Next i
MsgBox "Debug mode: " & blnDebug
MsgBox "Filename: " & strFilename
End Sub

Resources