2

So I created an .exe file (tlsolver.exe) to run using VBA (TLSolver.xlsm). When I launch the .exe, it runs some calculations, spits out a csv file, and then I use VBA to copy that data onto my excel sheet.

This is the VBA code I am using:

Public Sub StartExeWithArgument()
    Dim strProgramName As String

    ActiveWorkbook.Save
    strProgramName = "C:\Users\My.Name\Desktop\Python\Tagless\tlsolver.exe"

    Call Shell("""" & strProgramName & """", vbNormalFocus)
End Sub

When I run the macro, the console window pops up as it should and then quickly closes. I managed to see this error before it closes:

IOError: [Errno 2] No such file or directory: 'TLSolver.xlsm'

I know that the .exe works perfectly fine when I doubleclick on the file regularly, so I am inclined to think that I am messing up something silly in the VBA.

Any help appreciated!

Edit: I know the sub is labeled as StartExeWithArgument, but there is no argument required, simply click and run.

Community
  • 1
  • 1
ploo
  • 667
  • 3
  • 12
  • 26
  • Quick question... Why are you getting the error message `No such file or directory: 'TLSolver.xlsm'` why `.xlsm`? Shouldn't it be `.exe`? – Siddharth Rout Nov 25 '14 at 20:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65643/discussion-between-ploo-and-siddharth-rout). – ploo Nov 25 '14 at 20:33
  • 1
    Post chat discussion: The exe is launching correctly from VBA. The exe was created using python and is unable to read from the .xlsm file. – Siddharth Rout Nov 25 '14 at 20:40

1 Answers1

4

The shell command is executing correctly. The exe launches and then looks for the .xlsm file in the current path. What is happening is that it is not able to find the TLSolver.xlsm in the current directory and hence the error IOError: [Errno 2] No such file or directory: 'TLSolver.xlsm'

Three suggestions in such a case.

  1. Change the directory in VBA using ChDir to the directory where the excel file resides and then launch the exe OR

  2. Place both files in the same directory. OR

  3. Rewrite the python exe code (This falls out of my expertise so can't help you here) to prompt the user to select the excel file.

VBA PART (Suggestion 1)

Public Sub StartExeWithArgument()
    Dim strProgramName As String
    Dim xlFilePath As String

    '~~> Path of the excel file. Change as applicable
    xlFilePath = "C:\Temp"

    ActiveWorkbook.Save

    '~~> Change directory
    ChDir xlFilePath

    strProgramName = "C:\Users\My.Name\Desktop\Python\Tagless\tlsolver.exe"

    Call Shell("""" & strProgramName & """", vbNormalFocus)
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250