Open DOS Program and execute keystrokes?

Cosmos75

Registered User.
Local time
Today, 05:21
Joined
Apr 22, 2002
Messages
1,281
Is there anyway to launch a DOS Program and execute some keystrokes?

There is a DOS program I need to use but it is cumbersome. It involves entering some data paramaters and letting the program calculate something. The problem is I have multiple scenarios and it is tedious to enter all that over and over again (sometimes changing only one value).

I was wondering if there is a way to launch the DOS program from Access and enter the values from a table automatically into the DOS program?
:confused:
 
as far as launching the program, easy use the Shell command.

However entering keystrokes is a different story.

I remember though in *REAL* DOS, back in the day, there was either a program that could do that, or it was done through batch files where you could enter keys to be hit in the command line parameters... can't remember anything about it though. Might have been something i downloaded... who knows O.o
 
Hi Cosmos,

Well until ghudson shows up (he'll solve it). Can you use SendKeys?
Or maybe Shell("SomeProgram < SomeFile.txt")?

I know I've seen the calculator shelled with SendKeys being used to
send it some formulas.

I'll let you know if I find anything.

Wayne
 
ReAn & WayneRyan,

Thank you both for your replies. I have never used Shell() before so I will try to find more on that.

THANKS!
 
for the shelling part:
Code:
Private Type STARTUPINFO
   cb As Long
   lpReserved As String
   lpDesktop As String
   lpTitle As String
   dwX As Long
   dwY As Long
   dwXSize As Long
   dwYSize As Long
   dwXCountChars As Long
   dwYCountChars As Long
   dwFillAttribute As Long
   dwFlags As Long
   wShowWindow As Integer
   cbReserved2 As Integer
   lpReserved2 As Long
   hStdInput As Long
   hStdOutput As Long
   hStdError As Long
End Type

Private Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessID As Long
   dwThreadID As Long
End Type

'Declarations
'============
Declare Sub SetWindowPos Lib "user32" _
    (ByVal hWnd As Integer, _
    ByVal hWndInsertAfter As Integer, _
    ByVal X As Integer, _
    ByVal Y As Integer, _
    ByVal cx As Integer, _
    ByVal cy As Integer, _
    ByVal wFlags As Integer)

Private Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32" _
    (ByVal lpApplicationName As Long, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function ShellExecute Lib "shell32" Alias "ShellExecuteA" _
    (ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

'Constants
'=========
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Const SW_SHOW = 5

Public Sub ExecAss(hWnd As Long, sCmdLine As String)
'Executes scmdline via file association ie c:\bootlog.txt will run notepad!
Dim Ret As Long

Ret = ShellExecute(hWnd, "open", sCmdLine, vbNullString, CurDir$, SW_SHOW)
If Ret < 32 Then
    'Error can't shell
    msgbox "Failed to execute: " & sCmdLine
End If
End Sub

Public Sub ExecCmd(sCmdLine As String)
'Executes cmdline and waits for it to finsh
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Dim Ret As Long

' Initialize the STARTUPINFO structure:
Start.cb = Len(Start)

' Start the shelled application:
Ret = CreateProcessA(0&, sCmdLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, Start, proc)

' Wait for the shelled application to finish:
Ret = WaitForSingleObject(proc.hProcess, INFINITE)
Ret = CloseHandle(proc.hProcess)
End Sub
for the control then as mentioned sendkeys is the answer.
 
cable,

Thank you for answering, will give that a whirl!
:)
 
Created a module (basShellDOS) an copied over cable's code.

So far I've only figure out how to start the program.
Code:
Call basShellDOS.ExecAss(1, "C:\TNMLook\TNMLook.exe")
But I haven't figured out how to send any keystrokes.

Tried both
Code:
Call basShellDOS.ExecCmd("{Enter}")
SendKeys "{Enter}", True
Here's what I need to do in the DOS program.
  1. DOS Program Starts
  2. Press Enter to Continue
  3. Name File
  4. (Please Wait) - Program does something, I think it creates a file or something.
  5. Enter Description
  6. Enter M or E
  7. Enter Y or N
  8. Enter Value (Integer)
  9. Enter Value (Integer)
  10. Enter Value (Integer)
  11. Enter Value (Integer)
  12. Enter Value (Integer)
  13. Enter Value (Integer)
  14. Enter Y or N
  15. Enter H or S
  16. Enter Value (Integer)
  17. Enter Value (Decimal)
  18. Enter Value (Integer)
  19. Enter Value (Decimal)
  20. Enter –1 (To Finish)
What am I doing wrong?
:confused:
 
did you make the dos program? or is it open source? wouldn't it be easyer to give it command line capability?
 
Try this code:

Code:
Dim retVal
retVal = Shell("C:\TNMLook\TNMLook.exe", 1)
AppActivate retVal

SendKeys "{Enter}", True
SendKeys "myfilenamehere{Enter}", True
SendKeys "mydescriptionhere{Enter}", True
SendKeys "MorEenteredHere{Enter}", True
'and so on.....
 
Code:
[COLOR=Red][B]AppActivate retVal[/B][/COLOR]
DOS Program opens but I get the following error message with the line above..

Run-time error '5':
Invalid procedure call or argument.

:(
 
Odd, help file for access seems to outline the function thoughoughly...

Um, try it without that line.
 
Nope, still not working.
:(

It actually opens the DOS Program twice now?!?! Then it enters a bunch of \s'...
:confused:

By the way, thank you so much for help me out on this! I really appreciate it!
 
That is truly odd.... really is, well im out of ideas, i wonder if anyone else knows what to do.
 
WayneRyan,

Thank you for that link! Almost the same as what is in my VB & VBA in a Nutshell book.

Still no luck with the SendKeys but I may be using it incorrectly.
WayneRyan said:
until ghudson shows up (he'll solve it).
[yell_for_help]ghudson![/yell_for_help]
:p

Haven't seen him online in awhile.
:(

Oh, well... I'll keep on trying other things and looking around some more. If I do find a solution (or if I find out that I can't do it), I'll post back here for others who may be wondering how to accomplish the same thing.
 
Cosmos75 said:
[yell_for_help]ghudson![/yell_for_help] :p

Haven't seen him online in awhile. :(
You make it sound like I know what I am doing. ;)

I avoid DOS and SendKeys like the plague [for odvious reasons]. :D

I do not have any DOS programs to play with but I would imagine that something as simple as the below code [I tested it and it does work] should work for you. This will open a new session of Excel, open a new workbook and type the work "Hello" in the first cell.
Code:
Call Shell("C:\Program Files\Microsoft Office\Office\Excel.exe", vbNormalFocus)
'Call Shell("C:\TNMLook\TNMLook.exe", vbNormalFocus)

AppActivate "Microsoft Excel" 'applications Window title

SendKeys "{F10}", True
SendKeys "F", True
SendKeys "N", True
SendKeys "{ENTER}", True
SendKeys "H", True
SendKeys "e", True
SendKeys "l", True
SendKeys "l", True
SendKeys "o", True
SendKeys "{ENTER}", True
What happens when you try this for your needs with the DOS program?

If the SendKeys stuff appears to be moving to fast for the DOS program then you could use the Sleep API to slow things down when needed.
 
Apparantly 'AppActivate' dosent exist in his version of access....

*Shrug* :confused:
 
ghudson said:
I avoid DOS and SendKeys like the plague [for odvious reasons]. :D
I hear you on that one!
ghudson said:
What happens when you try this for your needs with the DOS program?
Changed the code slightly to this
Code:
Call Shell("C:\Program Files\Microsoft Office\Office\Excel.exe", vbNormalFocus)

AppActivate "Microsoft Excel" 'applications Window title

SendKeys "H", True
SendKeys "e", True
SendKeys "l", True
SendKeys "l", True
SendKeys "o", True
SendKeys "{ENTER}", True
That does enter "hello" in cell A1, the original version ending up with the Find box ready to search for "Find". I think my Excel opened up automatically with a new workbook.

So AppActivate is ok for my version of Access (2000). Timing issues aside, why do I get an error with that?
:confused:
 

Users who are viewing this thread

Back
Top Bottom