2

I have a Matlab-generated executable file, Myfile.exe to call from . I learned (Shell Function) is what I need to use.

I don't want to include the whole file path as I do not want to restrict the user to a certain folder in a certain location on each computer.

I have the following code to call the executable, which works fine:

Sub MyExe()
    On Error Resume Next
    Shell ("C:\Users\elwany\Desktop\Myfolder\Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    End If
End Sub

My problem/question is I put the executable + the Excel file with the VBA project in the same folder (Myfolder), and then I modify the code to:

Sub MyExe()
    On Error Resume Next
    Shell ("Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    End If
End Sub

Sometimes it works, sometimes it doesn't!

For example, yesterday I ran the VBA code, it worked. Today I opened the same Excel file, same folder, same everything, it gives "Can't Start Application" error msg!!

  1. Is it not okay to remove the file path even if I have everything in one folder?
  2. Why does it sometimes work, sometimes not?
  3. Is adding the file path absolutely mandatory?
1
  • For future reference you can change the current directory with ChDir. so ChrDir ThisWorkbook.Path prior to Shell ("Myfile.exe") would work in this case. Commented Jan 8, 2012 at 2:07

2 Answers 2

2

As you have enquire further about different directories note that you can either

  1. Use ChDir as per my earlier comment to your question
  2. Use Dir instead to validate that myfile.exe is where it needs to be. This method doesn't need error handling to handle the file being missing.

    Sub TestB()
    Dim strPath As String
    strPath = Dir("c:\temp\myfile.exe")
    If Len(strPath) > 0 Then
        Shell strPath
    Else
        MsgBox "Path doesn't exist"
    End If
    End Sub
    
    
    Sub TestA()
    On Error Resume Next
    'use the host workbook path
    ' ChDir ThisWorkbook.Path
    'set path here
    ChDir "C:\temp"
    Shell ("Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    Else
        MsgBox "sucess!", vbOKOnly
    End If
    End Sub
    
Sign up to request clarification or add additional context in comments.

1 Comment

Brettdj, Thanks a lot for your input! really appreciate it.
2

When you run a shell like this without a path specified it runs from the Active Directory. What the Active Directory is depends on the OS, not Excel/VBA (unless you explicitly set it)

Try this instead

Sub MyExe()
    On Error Resume Next
    Shell (ThisWorkbook.Path & "\Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    End If
End Sub

1 Comment

Thanks Chris, I got your point! Your code works. how do we set the active directory, then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.