PDA

View Full Version : "file not found" when using Shell()



secret_sauce
03-23-2006, 03:13 AM
Forgive if this is a total noob question.

All I want to do is launch an external app (a Perl script) using Shell(). But all I get is "Run-time error '53': File not found."

I even copied the example verbatim from the VB Help:

Dim RetVal
RetVal = Shell("Macintosh HD:Applications:Calculator", vbNormalFocus)


Same error. This seems so elementary.

mansky
03-25-2006, 10:00 AM
Hi,
Try using the function MacScript() instead of Shell(). I believe Shell isn't operational on the Mac version of Office, but MacScript is the equivalent.
To run a Perl script, or any other script or binary, you'll need to use "do shell script" in Applescript. If you need examples, post back. There's a bit of overhead in calling Perl scripts, via Applescript, from VB in any Office application, but for normal tasks, the extra time shouldn't be a problem.


Good luck!

Ed

secret_sauce
03-25-2006, 03:46 PM
Excellent, Ed. Pointing me to MacScript() was the tip I needed. Found a post elsewhere started by ... you! This seems to work nicely:

sPath = "/Users/Shared/Scripts/ttt.pl"
sSwitches = "-mt -L French"
sArgs = "/Users/fred/Documents/foo"

sCmd = "set RetVal1 to do shell script """ & sPath & " " & sSwitches & " " & sArgs & """"
RetVal2 = MacScript(sCmd)

Only problem: If the script has any problems (a bad path, a bad permission), the macro fails with Runtime Error '5', so the error checking in the script itself seems to be bypassed. Should I be intercepting the VBA runtime errors somehow, or do I need to do some prelim. checking of paths and permissions before I call the script?

mansky
03-25-2006, 05:14 PM
Hi,
You could check for the existence of the file and/or the path in VBA before you go and run your Perl script, or you can put the checks for existence in the Perl script itself. Then have the VBA macro calling the Perl script do various things depending on the return value from the Perl script.

Generally, I'd go with the first approach simply 'cause it'll result in "cleaner" code.

Also, I would recommend using an Error Handler statement in your VBA to handle any errors encountered while running the macro. Otherwise the end User gets a message from Word they may or may not know what to do with. If the problem is simply the existence of a file or path, then I'd check for the existence of them first, before calling the Perl script. In the case where the indicated file/path doesn't exist, you can pop up a MsgBox on the User's screen alerting them to the situation and let them proceed forward by taking the indicated action. Check out the "On Error" VBA help entry for more details.

A quick boolean test for the existence of a file is:

Function FileExists(Filename As String) As Boolean
On Error Resume Next
FileExists = (Dir$(Filename <> "")
End Function


Ed

secret_sauce
03-30-2006, 01:39 AM
Excellent, Ed. Thanks.

Last question, how do I rename the subject line of this thread to show solved solved? I only get an Edit button on my last post, not the first one (with the subject line).

mansky
03-30-2006, 05:21 AM
Hi,
After the last site upgrade, the "Mark Thread Solved" button is missing. As far as I know, it is being worked on and will be added back by the site admin when the upgrade is complete. You can see the progress on the software upgrade by clicking the thread "The site is being upgraded..." at the top of the page.


Ed