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
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