Running Autoit script from Excel VBA - excel

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" & "'"

Related

Running Exe File from CMD using VBA 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 ...

VBA: Help running cmd line from excel vba

I use a program that extracts data from pdf files and you can use cmd line to control the program, i would like to intergrate this with my excel sheet, the code below works when pasted in to cmd just fine but when i try from VBA it isnt working.
this is what ive managed to find online, help will be very appreciated.
sCommandToRun = "C:\Program Files (x86)\A-PDF Data Extractor\PDECMD.exe" -R"Accord new" -F"C:\Users\phill\Desktop\Test.pdf" -O"C:\Users\phill\Desktop\results.xlsx" -Txlsx -PA
Call Shell("cmd.exe /S /C" & sCommandToRun, vbHide)
aslo if there is a way to wait until the cmd line has finished before i excute another line of code would also be a big help.
Thanks
Add quotes around the paths with spaces in them.
Option Explicit
Sub test()
Dim sCommand As String
sCommand = """C:\Program Files (x86)\A-PDF Data Extractor\PDECMD.exe""" & _
" -R""Accord new""" & _
" -F""C:\Users\phill\Desktop\Test.pdf""" & _
" -O""C:\Users\phill\Desktop\results.xlsx"" -Txlsx -PA"
'Debug.Print sCommand
Shell "cmd.exe /S /C " & sCommand, vbHide
End Sub
changing '"Shell "cmd.exe /C " & sCommand, vbHide" to "Shell sCommand, vbHide" did the trick some some reason – phill cook just now Edit

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?

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

Ms Excel VBA - Using Shell command to open files

I am developing a workbook in Excel-VBA and i want to execute a script using HEC DSS. That means, using excel-vba, i need to open "HEC DSS" first and then i'll instruct the application to open the script.
We keep it simple and try to correlate the above scnerio with a NotePAd.exe and a txt file. For the purpose, i have googled and tried different things but none worked. I am trying to use the SHELL command. Please find the code below:
Sub test()
Dim retval as string
dim file name as variant
filename="C:\Users\Nayar Asif\Desktop\Test_2.txt"
retval = Shell("notepad.exe" & filename, vbnormalfocus)
end sub
The above code does not work. The idea is to open the notepad application and then open the notepad file. Any help????
Regards
Nayyar
File paths with spaces should be in quotes
retval = Shell("notepad.exe """ & filename & """", vbnormalfocus)
The solution is just the "space" after notepad.exe and before the second quote :
retval = Shell("notepad.exe " & filename, vbnormalfocus)

Resources