Reputation: 45
i want to write a macro that open some external application using shell command, wait for exact amount of time then close that application and open new, also from command line, my problem is that the code doesn't execute after shell command taskkill. I checked that it work if run macro in debug mode, anyone has idea what is wrong?
Retval = Shell("C:\Program Files\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe -I -R C:\Users\Pavel\Documents\Ansys_import\basic_1.wbjn")
Application.Wait (Now + TimeValue("00:02:00"))
Retval = Shell("taskkill /f /im AnsysFww.exe")
newpar = Shell("C:\Program Files\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe -I -R C:\Users\MARCIN\Documents\Ansys_import\basic_2.wbjn")
Upvotes: 1
Views: 364
Reputation: 71187
Debugging around the edges and seams of a system is always non-trivial, and that's precisely what's going on here: nothing is wrong with your code as far as VBA is concerned, other than you don't need to capture a function's return value (e.g. RetVal
) if you aren't going to use it later.
The problem is with the arguments given to the Shell
command.
Grab the entire command string (without the delimiting quotes), and copy it.
C:\Program Files\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe -I -R C:\Users\MARCIN\Documents\Ansys_import\basic_2.wbjn
If the string was built/concatenated from string literals and variable values, the easiest way to get the command string into the clipboard is to separate the responsibility of coming up with a command string and executing the shell command, by introducing a local variable:
Dim cmd As String
cmd = "..." & "..." & "..."
Shell cmd ' place breakpoint here (F9)
When the breakpoint is hit at run-time, bring up the immediate pane (Ctrl+G) and output the value of the cmd
variable:
?cmd
C:\Program Files\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe -I -R C:\Users\MARCIN\Documents\Ansys_import\basic_2.wbjn
And now you can copy it from there.
Next, fire up cmd.exe
and paste the command string; expect an error message.
The error is, as Tim Williams hinted earlier, related to whitespace in the path: you'll want to quote it:
"C:\Program Files\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe" -I -R C:\Users\MARCIN\Documents\Ansys_import\basic_2.wbjn
If this works in cmd.exe
, then you know how to fix your VBA code - remember that because "
double-quotes delimit string literals, you need to double them in order to escape them:
cmd = """C:\Program Files\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe"" -I -R C:\Users\MARCIN\Documents\Ansys_import\basic_2.wbjn"
Lastly, the C:\Users\MARCIN\Documents
part of the path should probably be replaced with %USERPROFILE%\Documents
, and C:\Program Files
with %PROGRAMFILES%
:
cmd = """%PROGRAMFILES%\ANSYS Inc\ANSYS Student\v192\Framework\bin\Win64\runwb2.exe"" -I -R %USERPROFILE%\Documents\Ansys_import\basic_2.wbjn"
Or you can use Environ$("USERPROFILE")
and Environ$("PROGRAMFILES")
in VBA to get the string values for these environment variables, if you want to have VBA do the variable lookup rather than cmd.exe
.
Upvotes: 2