Help my rubbish VBScript

Soldato
Joined
27 Aug 2004
Posts
2,955
Location
Singapore ExPat
Have tried knocking this little script up this morning, it kinda works except you can't cancel out of it if you want to...what am I missing? Something in the Loop or the If statement is ruining it I think...

Code:
'Simple script to open a telnet session with a chosen server

Option Explicit
Dim objShell, intCount, strServer, strInput


' Input Box to get name of server to connect to
'Do 
strServer = (InputBox(" Server to connect to:", "CRT Replacement"))
     If strServer <> "" Then
     strInput = True
     End if
'Loop until strInput = True

'Open command prompt

Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd" 
WScript.Sleep 1000
objShell.AppActivate "C:\windows\system32\cmd.exe"

'send relevant commands to window.

objShell.SendKeys "telnet" 
objShell.SendKeys "{ENTER}" 
objShell.SendKeys "o " 
objShell.SendKeys strServer
objShell.SendKeys "{ENTER}"
 
It's pieced together from other scripts, I left it in just in case I might need it later, is that causing the problem?
 
I'm not entirely sure I know what you are intending to do. for a start the loop would be better changed to:

Code:
'Keep looping unless the user inputs a server name
Do
    strServer = (InputBox(" Server to connect to:", "CRT Replacement"))
Loop Until strServer <> ""

If you want to keep repeating the input box until something is typed.

The rest of the code just creates a cmd box and tries to log onto the telnet server specified by the user.
 
Yep exactly, it just telnets onto a server, it works fine except if you press cancel it just keeps looping. What I want is if you press Cancel for it to just exit out of the script entirely, if I remove the loop and press cancel it cancels the input box but the rest of the script continues. Sorry, as you can see I am not very good at scripting...
 
You could use a msgbox to ask the user if they want to quit the application, if the msgbox returns true then use:

Code:
objshell.application.quit
set objshell = nothing

to quit the command box and clear the activex object.

If that does not work then just use the following:

Code:
objShell.SendKeys "exit" 
objShell.SendKeys "{ENTER}"

which will send the exit command to the cmd box.
 
this should help:

there is a difference between empty(cancel), and zero length(enter)


If IsEmpty(strServer) Then
MsgBox "No data entered", vbExclamation, "Cancel Pressed"
ElseIf Len(strServer) = 0 Then
MsgBox "No Data Entered.", vbInformation, "OK pressed"
Else
MsgBox "Data Entered: '" & strServer & "'.", vbInformation, "OK pressed"
End If
 
Back
Top Bottom