Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using wscript in VBA 2

Status
Not open for further replies.

biketech

MIS
Jul 19, 2004
65
US
I'm hoping this is quick and easy. This scripts seems to work OK as a VBS script but not in VBA. I just want to open a shull, run an executable which logs into a remote system, and then send "exit" to the shell so it quits. Works ok in VBS, but in VBA, I get an "Object Required" error on the sleep line.

Thanks in advance...

Code:
Private Sub Command0_Click()

Dim WSHShell As Object

Set WSHShell = CreateObject("Wscript.Shell")
WSHShell.Run ("c:\plink -1 -i c:\privkey.ppk -l bob@host.com host.com")
Wscript.sleep 5000
WSHShell.SendKeys "exit"
WSHShell.SendKeys "{Enter}"

End Sub
 
Hi,

This might work for you
Code:
Private Sub Command0_Click()

   Dim WSHShell As Object
   
   Set WSHShell = CreateObject("Wscript.Shell")
   With WSHShell
      .Run ("c:\plink -1 -i c:\privkey.ppk -l bob@host.com host.com")
      p = 5000
      t = Timer
      Do While t + p < Timer
         DoEvents
      Loop
      .SendKeys "exit"
      .SendKeys "{Enter}"
   End With
End Sub

Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
Thanks Skip. That worked really well, although I think for the Do While loop you meant to type:

Code:
Do While Timer < t + p

One more question - if the shell window loses focus, then the SendKeys method just sends to whatever window has the focus (for example, the code window when I'm stepping through in Debug mode). Is there a way to set the focus back on to the shell window before using SendKeys? WSHShell.SetFocus is not supported.

Thanks again.
 
Maybe .Activate or .Select???

Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
Take a look at the AppActivate method.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yup. AppActivate does the trick. Now the code looks like this:

Code:
Private Sub Command0_Click()

Dim retval As Integer

retval = Shell("c:\plink.exe -1 -i c:\privk.ppk -l bob@host.com host.com", 6)

p = 5
t = Timer
Do While Timer < t + p
   DoEvents
Loop
   
MsgBox "Press Ok to continue", vbOKOnly 'Just to demonstrate losing focus
   
AppActivate retval
SendKeys "e", True
SendKeys "x", True
SendKeys "i", True
SendKeys "t", True
SendKeys "{Enter}", True

End Sub

I had to break up sendkeys into individual letters, otherwise the entire word "exit" doesn't make it to the ssh shell for some reason. Kind of dirty looking but it works.

Thanks Skip and PHV.

BikeTech.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top