Consulting

Results 1 to 6 of 6

Thread: "file not found" when using Shell()

  1. #1

    Question "file not found" when using Shell()

    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:

    [VBA]Dim RetVal
    RetVal = Shell("Macintosh HD:Applications:Calculator", vbNormalFocus)[/VBA]


    Same error. This seems so elementary.
    Last edited by secret_sauce; 03-23-2006 at 11:35 AM.

  2. #2
    VBAX Regular
    Joined
    Jan 2006
    Posts
    56
    Location

    MacScript

    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

  3. #3
    Excellent, Ed. Pointing me to MacScript() was the tip I needed. Found a post elsewhere started by ... you! This seems to work nicely:

    [VBA]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)[/VBA]

    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?

  4. #4
    VBAX Regular
    Joined
    Jan 2006
    Posts
    56
    Location

    Existence and Error recovery

    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:
    [VBA]
    Function FileExists(Filename As String) As Boolean
    On Error Resume Next
    FileExists = (Dir$(Filename <> "")
    End Function
    [/VBA]

    Ed

  5. #5
    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).

  6. #6
    VBAX Regular
    Joined
    Jan 2006
    Posts
    56
    Location

    Thread solved button

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •