PDA

View Full Version : Running a .bat file from VBA



FrancoisStev
02-21-2015, 12:37 AM
I am writing a VBA program that puts a list of commands in a .txt file. These commands are used in a program called xfoil to do calculations for me and puts the results in an output.txt file.

I made a Run.bat file that when I click on it executes perfectly and do everything I want it to do. The problem I am having is that when I use the Shell command in vb then there briefly appears a window saying specified file not found and then nothing happens.

Private Sub CommandButton1_Click()

FileNum = FreeFile

Open "C:\Users\Francois\Desktop\New folder\Input.txt" For Output As #FileNum

Print #FileNum, "naca1012"
Print #FileNum, "plop"
Print #FileNum, "G"
Print #FileNum, ""
Print #FileNum, "OPER"
Print #FileNum, "VISC 245000"
Print #FileNum, "pacc"
Print #FileNum, "Out.txt"
Print #FileNum, ""
Print #FileNum, "a 5"
Print #FileNum, "pacc"
Print #FileNum, ""
Print #FileNum, "Quit"
Close #FileNum

Call Shell("C:\Users\Francois\Desktop\New folder\Run.bat", 1)


End Sub


Can someone please help?

snb
02-21-2015, 06:30 AM
Private Sub CommandButton1_Click()
Open "C:\Users\Francois\Desktop\New_folder\snb.bat" For Output As #1
Print #1, replace("naca1012~plop~G~~OPER~VISC 245000~pacc~Out.txt~~a 5~pacc~~Quit","~",vbcrlf)
Close

Shell "C:\Users\Francois\Desktop\New_folder\snb.bat",0
End Sub

NB. Avoid spaces in foldernames/filenames

FrancoisStev
02-21-2015, 07:02 AM
It is still not running. The window opens and closes so fast that I can not read the error. Is there some way to slow it down?

The code in my Run.bat file is:
xfoil.exe < Input.txt

xfoil is a command based program. The commands to do a certain calculation is in the Input.txt file.
The Run.bat works fine if I click on it; it runs xfoil and executes fine. I does not want to work when I use the shell command. I have removed all the spaces as suggested.

snb
02-21-2015, 08:31 AM
in that case you don't need a bat file


Private Sub CommandButton1_Click()
Shell "xfoil.exe < """ & replace("naca1012~plop~G~~OPER~VISC 245000~pacc~Out.txt~~a 5~pacc~~Quit","~",vbcrlf) & """",0
End Sub
or

Private Sub CommandButton1_Click()
Shell "xfoil.exe """ & replace("naca1012~plop~G~~OPER~VISC 245000~pacc~Out.txt~~a 5~pacc~~Quit","~",vbcrlf) & """",0
End Sub

FrancoisStev
02-21-2015, 10:02 AM
Thanks. my program is running now.