Running Exe File from CMD using VBA Excel - excel

I am trying to run a cmd line from VBA. The command line calls a createReport.exe which creates a final CSV output file using Inputfile.csv
This is what I run manually from Command prompt window:
cd C:\Users\user123\Desktop\MyReport_folder (hits enter)
createReport.exe
-in=C:\Users\user123\Desktop\MyReport_folder\Inputfile.csv (hits enter)
When I run manually, it takes around 45 seconds to create final CSV output file.
When I run the same thing from VBA code, the screen says "starting the query step" and it stays on for 30 seconds, closes and doesn't create the final CSV output file.
Sub RunReport()
Application.DisplayAlerts = False
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:\Users\user123\Desktop\MyReport_folder\createReport.exe"
strArgument = "-in=C:\Users\user123\Desktop\MyReport_folder\Inputfile.csv"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbMaximizedFocus)
Application.DisplayAlerts = True
End Sub

I believe you first need to do a ChDir before calling the Shell() command, something like:
ChDir "C:\Users\user123\Desktop\MyReport_folder"
Call Shell("""" & strProgramName ...

Related

open .exe in VBA excel and write in the input window

I have to run an .exe in VBA Excel and write in the input window "in.txt" "out.txt" in order to make the process automatic inside a macro. I tried to use shell but it works asynchrounous and I also don't know how to tell her to write inside the .exe.
I've also tried with SendKeys but apperently it doesen't work.
How could I make the VBA calling my .exe, open it, write inside the command window of the .exe, wait for the output and use it to go on?
thank you in advance
here are two attempts (both failed):
Sub write()
prog = Shell("C:\Users\arancia\Pictures\Camera Roll\axtur\axtur\AXTUR_64.exe", 1)
Application.Run "'AXTUR&EXCEL.xlsm'!inserisci_dati_input"
SendKeys.send "in.txt~", True
SendKeys.send "out.txt~", True
SendKeys "%{F4}", True
End Sub
Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:\Users\arancia\Pictures\Camera Roll\axtur\axtur\AXTUR_64.exe"
strArgument = "in.txt~out.txt~"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub
One solution would be to write a batch file that includes all the parameters and then run the batch.
I have used WshShell (Windows scripting host) to run batch files to do what you want in the past but WshShell does not work on our computers since the Nov 2020 updates. WshShell allows you to wait for the outcome of the external program.
One way I found to go around it is to write a simple text file at the end of the batch and wait for it to show up. This is crude but it works.
In the code below, I write a simple batch file in the folder of the Excel sheet. The last line of the batch writes the content of the folder in a text file. The Do Until loop waits for the text file to show up in 1 second increments. When the code resumes after the loop, the text file is deleted. If you write the command line you would type in cmd instead of "echo Hello World" this should work.
You need to reference the Microsoft Scripting Runtime (scrrun) to use the file system object.
Good Luck!
Public Sub RunBatch()
Dim i As Integer
Dim k As Integer
Dim xlWB As Workbook
Dim fso1 As New FileSystemObject
Dim BatFile As Object
Dim IsDone As Boolean
Dim OutFileName As String
Set xlWB = ThisWorkbook
OutFileName = xlWB.Path & "\" & "HW.bat"
Set BatFile = fso1.CreateTextFile(OutFileName)
BatFile.WriteLine "cd /d " & xlWB.Path
BatFile.WriteLine "echo Hello World"
BatFile.WriteLine "dir > Done.txt"
BatFile.Close
IsDone = False
Call Shell(OutFileName, vbNormalFocus)
Do Until IsDone
If fso1.FileExists(xlWB.Path & "\Done.txt") Then IsDone = True
Application.Wait (Now + TimeValue("00:00:01"))
Loop
fso1.DeleteFile (OutFileName)
End Sub

Record in a .txt file the generated results of an .exe

I have and Excel file which contains the data for a Python code. Python code is called from a .exe, and I can see in the PowerShell the results which that code is generating.
Sub Button()
Dim strPName As String
Dim strData As String
strPName = Range("H24").Text
strData = "--inputs"
Call Shell("""" & strPName & """ """ & strData & """", vbNormalFocus)
End Sub
When I have issues in the code or with the data input, the PowerShell is switch off and I cannot review which was the issue.
I want to include in the button code an automatic .txt file generation, which records the PowerShell text and let me review it after the stop.
I found here How to save the ouput of shell command into a text file in VBA the following command:
Shell "ftp -n -i -g -s:" & vPath & "abc.txt " & vFTPServ & " >> test.txt", vbNormalFocus
But I don't know how to connect it properly in my current button.
Could you give me some suggestions of how could I solve it?

How do I open this application using VBA

I am trying to debug the following code :
Public Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strArgument = ""
'Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
Call Shell(Application.ActiveWorkbook.Path & "\eclipse.exe ", vbReadOnly)
End Sub
This code runs when I click on a particular button in excel
However, when I run the code/ click the button, I get the following error:
Run time error 5
Invalid procedure call or argument

Macro prompts me to choose file even when its open

I have hard coded file name (old.xls) in my macro (in another workbook). old.xls is already open when I execute the macro. Even then the below line prompts me to choose a file.
.Formula = "=IF(AND(ISNUMBER(VALUE(MID(Q1, 2, 1))),LEFT(TRIM(Q1), 1) = ""R""),VLOOKUP(Q1 & "" *"",[old.xls]Sheet1!$D:$V,19,0),VLOOKUP(Q1,[old.xls]Sheet1!$E:$V,18,0))"
Please advise. Why isn't the macro referring old.xls which is already open? Why does it ask me to choose old.xls every time?
Every time I run the macro Excel prompts me with an open file menu.
I am pretty sure you should test full reference:
Dim test As String
test = ThisWorkbook.Path & "\"
'your WITH block code...
.Formula = "=IF(AND(ISNUMBER(VALUE(MID(Q1, 2, 1))),LEFT(TRIM(Q1), 1) = ""R""),VLOOKUP(Q1 & "" *"",'" & test & "[old.xls]Sheet1'!$D:$V,19,0),VLOOKUP(Q1,'" & test & "[old.xls]Sheet1'!$E:$V,18,0))"
If still there is a prompt I would close old.xls file and check simple file open:
Sub openTest()
Dim test As String
test = ThisWorkbook.Path & "\old.xls"
Workbooks.Open (test)
End Sub

Running Autoit script from Excel VBA

Im trying to run autoitscript from excel vba using a button click.
I wanted to run notepad1.au3 script from an excel sheet.
i copied the script into the same directory as excel sheet.
I wrote the following vba code to run the script.Everything seems to work fine, it takes the path file name etc accurately.
But instead of just running the script, an explorer window pops up ,asking me to locate the script i want to run.
I can browse to the location of the script through the explorer window and select the script file and it will run.
But i want it to run without opening an explorer window.
Any idea where could be the problem?
Thanks
VBA Code:
Sub Autoit()
Dim AutoItPath
Dim FileName As String
Dim FileName1 As String
FileName = ThisWorkbook.Path & "\notepad1.au3"
MsgBox (FileName)
AutoItPath = "C:\Program Files (x86)\AutoIt3" & "\AutoIt3.exe "
MsgBox (AutoItPath)
FileName1 = """" & AutoItPath & """" & """" & FileName & """"
MsgBox (FileName1)
runscript = Shell(FileName1)
End Sub
Your call is wrong. See the help file:
Run a script using the interpreter:
AutoIt3.exe [/ErrorStdOut] [/AutoIt3ExecuteScript] filename [params ...]
Execute the AutoIt3 script 'filename' with optional parameters
example command: "'" & "C:\Program Files (x86)\AutoIt3\AutoIt3.exe " /AutoIt3ExecuteScript "..path\notepad1.au3" & "'"

Resources