Consulting

Results 1 to 5 of 5

Thread: Running a .bat file from VBA

  1. #1

    Running a .bat file from VBA

    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?

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  3. #3
    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.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  5. #5
    Thanks. my program is running now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •