Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoke an .exe file using macro in excel

Status
Not open for further replies.

rk_19

Structural
Aug 7, 2012
71

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 = Left(FileNAME, Len(FileNAME) - 4) ' REMOVE XLS EXTENSION
'FileNAME = FileNAME & "csv" ' ADD CSV EXTENSION
FileNAME = "data.csv" ' any name can be set here
Application.DisplayAlerts = False ' REMOVE DISPLAY MESSAGE: PREVIOUS FILE WILL BE ERASED
ActiveWorkbook.SaveAs FileNAME:= _
MyPATH & "\" & FileNAME, FileFormat:=xlCSV, _
CreateBackup:=False


'
OldPath = CurDir
'


' Call ChangeCurrentDir(ActiveWorkbook.path, stat)
RetVal = Shell(FilePath, vbNormalFocus)
'Call ChangeCurrentDir(OldPath, stat)
'ActiveWindow.Close
'Application.DisplayAlerts = True ' Restore DISPLAY MESSAGE
End Sub
 
 http://files.engineering.com/getfile.aspx?folder=ff56eb52-a20a-4e0d-b1c0-4b0c76845ded&file=Capture.PNG
Replies continue below

Recommended for you

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)
 
@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' .
 
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)
 
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?
 
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:


 
@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..
 
@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
 
@irstuff, what lines should I add to my code for the vba to pass the path to the exe
 
@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)
 
 http://files.engineering.com/getfile.aspx?folder=4c935790-b3d4-4474-91f8-0de27f2d2e71&file=2017-05-03_at_17-12-24.mp4
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
 
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?
 
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.
 
@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 ?
 
If you are using IVF, try CHANGEDIRQQ. If you are using gfortran try CHDIR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor