How To Run Fortran.exe using Excel

How To Run Fortran.exe using Excel

Hello everybody,

I have a fortran.exe (compiled using intel visual fortan comipler 10.1.24 and VS 2005) which takes an input.dat file and generates output.dat.

Currently, I am trying to control fortran.exe execution with excel 2010 shell function. But it is not working properly.

The Shell function syntax is correct, it does run fortran.exe and DOS window can be seen. But fortran.exe ends prematurely without generating output.dat file. However, In case of double-clicking fortran.exe, it runs and reads input.dat and generate output.dat properly.

I have spent several days on this problem. I have recompile program, relaxed excel-marco security setting etc, but in vain.There are similar posts on web describing such problem, but no one clearly described the solution.

Surprisingling, using same shell function i can successfully run a java.exe file.

Any kind of comments, suggestion, advice regarding above problem will be warmly welcomed.

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

If you simply invoke a program using a shell function, it runs in the context of the executable's folder, which in this case would be that of excel.exe. If the Fortran program needed to write anything in the default directory, it couldn't. But if you are using redirection properly, this should not be an issue.

What is the exact invocation you're using in Excel?

Retired 12/31/2016

When you say prematurely do you mean before output.dat is finished your vba code looks for it or that it is not created at all? The former case may be the async shell call needing a waiting process like
http://www.cpearson.com/excel/ShellAndWait.aspx

Dear Steve,
Below see how I am trying to execute fortran.exe. Both fortran.exe and Excel-vba are in the same folder.
The variable "RetVal" shows value when this vba-code runs, but, there is no output generated.

'''''''''''''''''''''' VBA Code for Invoking "Fortran.exe"'''''''''''''''''

Dim FilePath, FindIt As String
Dim RetVal as varient

'Location of fortran.exe in current working folder path
FilePath = ActiveWorkbook.Path & "\fortran.exe"

'Fortran executable invoking using "Shell" Function
RetVal = Shell(FilePath , 1)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
To confirm the exact executable location, I made a batch file where I wrote current working folder path and fortran.exe location (i.e. F:\Temp\Fortran.exe, which same as in vba-code variable FilePath = F:\Temp\Fortran.exe ), It perfectly runs the executable in this case.

Well, appreciate your cooperation.

Dear mwindham,

"Prematurely" mean the former case as you indicated.

Actually, I have already tried that "ShellandWait" function on cpearson website, and It did not work for my case.

As indicated in reply #1 - the current working directory for the excel (and hence your fortran) process may not be what you expect.

If you start Excel from its icon in the program menu then I think the current directory is set to the equivalent of your "My Documents" folder on XP, which may not be the folder that contains the spreadsheet and fortran exe, contrary to your first comment.

If you do not have absolute paths for your input/output file names then your Fortran program may not be able to find them/put them in the right location.

Within VBA you can use the CurDir function to query the current directory and the ChDrive and ChDir statements to change it. Perhaps:

[vb]Option Explicit

Public Sub ExecuteMyProgram()
Dim FilePath As String
Dim OldPath As String
Dim RetVal As Variant
Dim stat As Integer
'****
FilePath = ActiveWorkbook.path & "Fortran.exe"
OldPath = CurDir
Call ChangeCurrentDir(ActiveWorkbook.path, stat)
RetVal = Shell(FilePath, vbNormalFocus)
Call ChangeCurrentDir(OldPath, stat)
End Sub

Public Sub ChangeCurrentDir(path As String, ByRef stat As Integer)
Dim fs As FileSystemObject ' From the Windows Script Host object library.
Dim the_drive As String
'*****************************************************************************
On Error GoTo ch_error
Set fs = CreateObject("Scripting.FileSystemObject")
the_drive = fs.GetDriveName(path)
ChDrive the_drive
ChDir path
On Error GoTo 0
stat = 0
Exit Sub
ch_error:
stat = 1
End Sub

[/vb] The current directory is a process wide thing, and can be changed by things like the File | Open dialogs and other VBA code, hence it is a good idea to not make persistent changes to it in a VBA procedure unless you really want to.

Note also, that your VBA code does not declare FilePath as a string! The as clause only applies to the variable name immediately before it, not to all the variables in the Dim statement.

Dear IanH,

Yes you right. There were several mistakes in my code. By using your code, my problem is solved. Really thankful for your cooperation.

Surprisingly, When I used my code for Java.exe, it perfectly works. Wondering, why did this problem happen in case of fortran.exe? Would you like comment on it? Any Idea?

On my computer java.exe is the executable file for the java virtual machine. It is on the system path and so can be found and run regardless of what the current directory is. Without arguments it doesn't care what directory it is run in.

Note that in your case the executable for your fortran program was probably being located and run, but I then suspect that it couldn't open its input file.

Leave a Comment

Please sign in to add a comment. Not a member? Join today