Run .bat file from Excel VBA, dynamically

raghuram.star

Board Regular
Joined
Sep 5, 2012
Messages
102
Hi

I have problem in running ".bat" files through VBA, but when I run ".bat" file it executing properly. but not through VBA. Below is the code I'm using

Code:
Sub RunBatFile()    Dim txtFpath, FilePath As String


    txtFpath = wksCmdJen.Range("C12").Value ' given path is C:\Elements
    FilePath = txtFpath & "\FetchElements.bat"

    ChDrive "C"
    RSP = Shell(Environ$("COMSPEC"), vbNormalFocus)
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys "CD " & txtFpath & "{ENTER}", True
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys "start " & FilePath & "{ENTER}", True
    Application.Wait Now + TimeValue("00:00:01")
End Sub

Another peace of code I used is
Code:
Call Shell("cmd " & FilePath, vbNormalFocus)

But none of them are working... I have used this code a long back, but not sure what I'm missing....

Is there a way to run a .bat file directly, without opening two CMD windows? or a way to close the first CMD window (which is been opened through VBA), after running the .bat file (second CMD window is closed using Exit Command with in .bat file)

Thank you
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
you don't need to use SendKeys at all. You want to write a BAT file containing all commands you need (cd, etc) and then call the BAT file.

Code:
Sub WriteAndRunBatFile()
Dim fs As Scripting.FileSystemObject
Dim file As TextStream
Dim retVal
Dim strFilePath As String

strFilePath = "C:\Elements\FetchElements.bat"

Set fs = New Scripting.FileSystemObject 'Requires a reference to Microsoft Scripting Runtime
'Set fs = CreateObject("Scripting.FileSystemObject") 'does not require the reference

Set file = fs.CreateTextFile(strFilePath)

file.WriteLine "blah-blah"
file.WriteLine "blah-blah"
file.Close

retVal = Shell(strFilePath)

End Sub

However, if you don't need to add anything to the existing batch file from Excel, just run it:

Code:
Dim retVal
retVal = Shell(strFilePath)
 
Upvote 0
@ poolhall

When I give the code
Code:
Dim retVal
Dim FileFolder, FileName, FilePath as String

FileName = "FetchElements.bat" '- FileName is constant which can be hard coded
FileFolder = "C:\ElementsFetched\" ' - FileFolder is user defined, which will referred to a cell in excel sheet as " = Sheet1.Range("B5").value

FilePath = FileFolder & FileName

retVal = Shell(FilePath)

Batch file is not executed. CMD window is just blinking and disappeared. Please let me know if I'm missing anything...
 
Last edited:
Upvote 0
This code is fine. To test it, I put the line "C:\windows\system32\calc.exe" in the batch file and Calculator opened. So, I guess there must be something wrong with your batch file's content.
 
Upvote 0
@ Poolhall

Thanks for the help, need to check it by altering the batch file. Hope for the best...

Thanks once again...
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top