Send Keys into dos shell

Rusty

Board Regular
Joined
Apr 8, 2002
Messages
108
Hi,

I'm attempting to code a macro that will allow me to launch a dos program and input parameters.

Initally I have a userform that the user can put variables into.

I then want to take these variables and use them as a string for with the dos program.

I'm attempting to use the "send keys" function, but am not having any joy with this at all.
Code:
Shell "C:\Windows\System32\cmd.exe", vbNormalFocus
    SendKeys ("Help me")

Except "Help me" would be:

myProgram myOption1 myOption2 myOption3


One workaround I could do is to create batch files, but I see this as a messy solution that I want to avoid.

Everything is fine, apart from getting dos to accept keys......

Has anybody got any advice on how to do this?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
Code:
Sub Tester()
Dim lRet As Long

lRet = Shell("C:\Windows\System32\cmd.exe", vbNormalFocus)
    
AppActivate lRet
Application.SendKeys ("Help me"), True

End Sub
 

Rusty

Board Regular
Joined
Apr 8, 2002
Messages
108
I tried this, but get a:

Run time error '5':

Invalid procedure call or argument


stepping through, the error occurs on the line:
Code:
AppActivate lRet
[/code]

Thanks for it anyway. I'll play with this a bit and try an get it working...
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
OK...just try


You may have to put a Wait there

Code:
Sub Tester() 
Dim lRet As Long 

lRet = Shell("C:\Windows\System32\cmd.exe", vbNormalFocus) 
    
Application.SendKeys ("Help me"), True 

End Sub
 

Rusty

Board Regular
Joined
Apr 8, 2002
Messages
108

ADVERTISEMENT

Hi Ivan,

As always, your help is greatly appreciated, this isn't the first time you've helped me out.

Many thanks.

-----

I was still getting a slight error on that code, in that it would launch the dos shell, but would transfer the text into excel instead.

I amended the code to put in a "wait" function to allow the shell time to load as such:

Code:
Sub Tester()
Dim lRet As Long

lRet = Shell("C:\Windows\System32\cmd.exe", vbNormalFocus)

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 5
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

Application.SendKeys ("Help me"), True

End Sub

So that part is all working good... just going to see if I can get my variables transported across now.
 

Rashers

New Member
Joined
Jul 24, 2003
Messages
31
Just wanted to thank Ivan and Rusty for this thread. I've been looking for the info. in these posts.
Cheers,
Andrew
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,348
see if you can adapt this
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

sub code()
ReturnValue = Shell("cmd.EXE", 1)
Sleep 500 ' wait 500 ms
AppActivate "C:\WINNT\System32"
Sleep 500
 SendKeys "cd\", True ' back to root of drive
 SendKeys "~", True
 Sleep 500
  SendKeys "c:", True ' switch to c drive
 SendKeys "~", True
 Sleep 500
 SendKeys "cd\", True ' back to root of c drive
 SendKeys "~", True
 Sleep 500
 SendKeys "cd winnt\system32", True ' this is where the cmd.exe file needs to be
 SendKeys "~", True
 Sleep 500
 mytext = "cscript " & fname
 SendKeys mytext, True  ' send script filename to command prompt
 SendKeys "~", True ' enter
  ' now close the DOS window
 AppActivate "C:\WINNT\System32"
Sleep 500
 SendKeys "exit", True
 SendKeys "~", True
  Sleep 1000
End Sub
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,565
Messages
5,765,147
Members
425,263
Latest member
alcat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top