Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Invoke an .exe file using macro in excel

Invoke an .exe file using macro in excel

Invoke an .exe file using macro in excel


Hi, i am trying to create an macro enabled excel file, in which , upon hitting the button, the excel gets saved to 'data.csv' into the same folder and an exe file is invoked (which is again in same folder) which will access the 'data.csv' for its inputs. i prepared this, however, upon hitting the button - i can an error as shown in attachment. the directory shown in that screenshot is the directory in which the exe was created initially - this directory has nothing to do with current working directory. could some one look into this. the code is shown below. i tried running the .exe file seperately and it ran seamless - so no issue with exe file - something to do with communication between macro and exe ??? thanks

Public Sub Save_CSV()
Dim MyPATH As String
Dim FileNAME As String
Dim FilePath As String
Dim OldPath As String
Dim RetVal As Variant
Dim stat As Integer
FilePath = ActiveWorkbook.path & "/dynjackup.exe"
OldPath = CurDir
MyPATH = ActiveWorkbook.path
FileNAME = ActiveWorkbook.Name
FileNAME = "data.csv" ' any name can be set here
ActiveWorkbook.SaveAs FileNAME:= _
MyPATH & "\" & FileNAME, FileFormat:=xlCSV, _

OldPath = CurDir

' Call ChangeCurrentDir(ActiveWorkbook.path, stat)
RetVal = Shell(FilePath, vbNormalFocus)
'Call ChangeCurrentDir(OldPath, stat)
'Application.DisplayAlerts = True ' Restore DISPLAY MESSAGE
End Sub

RE: Invoke an .exe file using macro in excel

I am a bit surprised you even get the error you posted, as the line:
FilePath = ActiveWorkbook.path & "/dynjackup.exe"
contains a fwd slash rather than backslash.

Your code also does not provide any parameters to the .exe file. It doesn't "communicate" with the .exe at all. It simply tells the OS to execute it. Although as I said I don't know how it doesn't throw an error there due to the fwd slash.

-handleman, CSWP (The new, easy test)

RE: Invoke an .exe file using macro in excel

@handleman, I think I something went wrong when I pasted it.. it is backslach in my code...may be I pasted one of my earlier codes in the forum , but the rest of code is matching my latest code. could you please help me .. how do I set the 'communication with exe' .

RE: Invoke an .exe file using macro in excel

You can't "communicate" with exe.

How does the .exe work? All your code is doing is same as double-clicking the .exe file in Windows. It's not specifying where the .exe should look for its data.

-handleman, CSWP (The new, easy test)

RE: Invoke an .exe file using macro in excel

As handleman said, you can't communicate with the exe.

Do you have access to the source code for the exe? Can it take a command line parameter? If not, can you add in code to take the command line parameter as the file you are generating?

RE: Invoke an .exe file using macro in excel

If the exe can be run from the command line, it will likely accept "arguments" (or command line switches) for input and will write its output to the "stdout" stream (and any error messages to the "stderr" stream). You can redirect these streams back to your VBA code to read the results from the exe. Here are a couple links to get started:



RE: Invoke an .exe file using macro in excel

@handleman, may be I was not clear with my question. my code activates my .exe file, but the exe is unable to find its input file (.csv) which is already in the same folder. I can see the .exe running and then immediately makes an exit as it is unable to find the .csv. if I run the exe (by manually double clicking it), there is no issue - it picks the csv properly - the whole things work effortless. the issue happens only when I activate the exe through macro..

RE: Invoke an .exe file using macro in excel

@irstuff, the package(.xlsm and .exe ) is to be distributed across various pc's. So can't assure the folder path, all we are sure is that they will be always together in a directory. Thanks

RE: Invoke an .exe file using macro in excel

@irstuff, what lines should I add to my code for the vba to pass the path to the exe

RE: Invoke an .exe file using macro in excel

@xwb. I prepared the .exe file (I wrote it in fortran) and it works perfectly when called from msdos prompt. but when I call from macro, the exe window opens and exits immediately and mentions that It was not able read the .csv file. The attached shows what is happening when I ran it from macro – at 00.03, the window flashes and if you are able to pause it (it says that unable to read the .csv – I don’t know why the .exe is unable to read the .csv while I run it from macro)

RE: Invoke an .exe file using macro in excel

rkr19 - I thought you started a thread on a similar topic in the spreadsheet forum, but I see it wasn't you, so I'll repost my response here:

Did you look at the link I posted in my first post in this thread?

It seems to me that it gives you everything you need to know to do what you want to do, but if it doesn't, please let me know what is lacking, or what doesn't work.

Also, have you tried stepping through the code in the VBA editor, to make sure it is generating the right text (path names, command lines etc)?

Doug Jenkins
Interactive Design Services

RE: Invoke an .exe file using macro in excel

You really need to get a clue and pass it along to us.

What exactly are you double-clicking when things work in the way that you won't describe to us?

How does the .exe expect to get the location of the .csv?

RE: Invoke an .exe file using macro in excel

Could you get the Fortran program to print the full file path of where it thinks the .csv file is? It may not be looking in the correct place.

If you are using IVF, have a look at GetModuleFilename and GetModuleHandle.

RE: Invoke an .exe file using macro in excel

@xwn, that makes sense. Currently My Fortran program automatically search for the 'current working folder' as I haven't specified any location to search the csv. Is there a way to explicitly specify the current location inside the Fortran code ?

RE: Invoke an .exe file using macro in excel

If you are using IVF, try CHANGEDIRQQ. If you are using gfortran try CHDIR

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close