SendKeys not sending to Cmd.exe

A

AltaEgo

Hi all
W7
XL2007
(same problem on XP. 2003)
Cannot use Cscript or Wscript.

I need to send some commands to a command window. Tested this code (and
variations). The Cmd window receives no keystrokes but the Msgbox pops up.




Sub test()
Dim ReturnValue
ReturnValue = Shell("CMD.EXE", 5)
'AppActivate ReturnValue
'will err when uncommented
SendKeys "ipconfig /all {ENTER}", True

MsgBox "done"

End Sub
 
P

Peter T

This worked for me -

Sub test()
Dim ReturnValue
ReturnValue = Shell("CMD.EXE", 5)

Application.OnTime Now + TimeSerial(0, 0, 1), "typeKeys"
End Sub

Sub typeKeys()
SendKeys "ipconfig /all {ENTER}", True

' MsgBox "done"

End Sub

Note SendKeys might not work at all in Vista.

Shame there doesn't seem to be a way to write the report to file. If so
could close the dos window and read the report back into VBA.

Regards,
Peter T
 
A

AltaEgo

Thank you. I now have it working using 'Sleep 500' (which is the same basic
idea). I guess the 'problem' stems from SendKeys commands not being
buffered by the command window.
 
D

Dave Peterson

Maybe you can use something like this:

dim myFileName as string
dim myParms as string

myfilename = "ipconfig"
myparms = "/all"

Shell Environ("comspec") _
& " /k " & Chr(34) & myFileName & Chr(34) & " " & myparms, vbNormalFocus


Use /c says to close that (hidden!) window when it's done.
The /k to see the command window (/k = keep open) (nice for testing).

I used /k and NormalFocus to test.

I'd use /c and vbHide when I was done testing.

But I would think you'd want to open the output of that ipconfig command.

Maybe something like:

Dim myFileName As String
Dim myParms As String

myFileName = "ipconfig"
myParms = "/all > C:\myfile.txt"

Shell Environ("comspec") _
& " /c " & Chr(34) & myFileName & Chr(34) & " " & myParms, vbHide

Would work ok.
 
P

Peter T

Excellent !

Regards,
Peter T

Dave Peterson said:
Maybe you can use something like this:

dim myFileName as string
dim myParms as string

myfilename = "ipconfig"
myparms = "/all"

Shell Environ("comspec") _
& " /k " & Chr(34) & myFileName & Chr(34) & " " & myparms,
vbNormalFocus


Use /c says to close that (hidden!) window when it's done.
The /k to see the command window (/k = keep open) (nice for testing).

I used /k and NormalFocus to test.

I'd use /c and vbHide when I was done testing.

But I would think you'd want to open the output of that ipconfig command.

Maybe something like:

Dim myFileName As String
Dim myParms As String

myFileName = "ipconfig"
myParms = "/all > C:\myfile.txt"

Shell Environ("comspec") _
& " /c " & Chr(34) & myFileName & Chr(34) & " " & myParms, vbHide

Would work ok.
 
A

AltaEgo

Thank you. I have a Q&D system working with shell (cmd) but this looks very
interesting. Hidden is a major bonus. I will revisit the problem using your
suggestion below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top