SendKeys and Path Problem

R

rdcord

Hello,

I (novice-Macro user) use a lot of fortran-compiled executables that
need a few inputs fed to them. Everything is in the same folder: the
Excel file, the executable, and the files fed to the executable. So I
created an Excel Macro that starts the executable with Shell() and then
I feed it the input files and other parameters using SendKeys(). I
have 2 problems:

1. SendKeys() is tempermental. I have found that I have to send it 2
letters at a time with a Sleep() delay so that the sending is not too
fast for the executable (I do not need the delay when I replace my
executable with say Notepad- only my executable has a pacing problem).
Is there a better way?
2. Based on an Excel cell I am taking a filename and sending it to the
Shell(). The filename refers to a file in the same directory as the
Excel file and the executable. The first time I run the program, it
crashes because it cannot find the file. If I save and close Excel
completely and then reopen Excel, the macro runs fine. I can get
around this by sending the entire path name to the executable, but
SendKeys() is slow. How can I get the Macro to know that the filename
is in the same directory as everything else? I have also tried putting
Application.DefaultFilePath=ThisWorkbook.Path' in my Workbook_Open
macro, but it does not help.

Here is a portion of my code:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)
Sub RunRunstream()
'
Dim ReturnValue As Variant
Dim rname As String
Dim i, ddelay, count As Integer
' I am not sure the following two lines do anything.
DefaultFilePath = ThisWorkbook.Path
Application.DefaultFilePath = ThisWorkbook.Path
i = 1
ddelay = Range("g10") * 1000
count = Range("h12") + 1
Do While i < count
Range("b3").Activate
rname = ActiveCell.Offset(i, 1)
' Executes Runstream.exe, pauses a little while and
' then calls the routine that sends the name.
ReturnValue = Shell(DefaultFilePath & "\Runstream", 1)
Call Sleep(ddelay)
Call SendName(i, rname, ddelay)
Range("b3").Activate
ActiveCell.Offset(i, -1) = "done"
i = i + 1
Loop
End Sub
Sub SendName(i, rname, ddelay)
DefaultFilePath = ThisWorkbook.Path
Application.DefaultFilePath = ThisWorkbook.Path
Call Sleep(ddelay)
kk = 1
' (This is why I would rather not send the path each time.)
Do While kk < Len(rname) + 2
Call Sleep(ddelay)
SendKeys Mid(rname, kk, 2), True
kk = kk + 2
Loop
Call Sleep(ddelay)
SendKeys "~", True
' Needed by the executable program.
Call Sleep(ddelay)
SendKeys "2", True
Call Sleep(ddelay)
SendKeys "~", True
Call Sleep(ddelay)
End Sub


Thanks very much,
Roger
 
J

Jim Rech

I don't understand why you are not feeding the input files and other
parameters via Shell, e.g,:

Shell "notepad.exe c:\test.txt", vbNormalFocus
 
R

rdcord

I cannot pass arguments to the executable on the command line due to the
nature of the executable and various prompts. In other words, even
from a dos prompt, I cannot pipe or feed the input file to the
program.

Thanks,
Roger
 
J

Jim Rech

I'd shoot the programmer then (hopefully he is not you<g>). The program
should have been written to prompt the user only if the command line is
empty.

SendKeys is usually not a good solution since it is unreliable and
influenced by outside events. I'd revised the EXE if at all possible.
 

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