VBA: Help running cmd line from excel vba - excel

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

Related

How to execute ipynb file in excel VBA code

I am trying to execute some python scripts through VBA code.
.py files are working pretty well, but i am struggling with .ipynb files
running python script(.py) as following link :
https://stackoverflow.com/questions/73258152/how-to-exceute-ipynb-file-in-excel-vba-code),
but the same way doesn't work for ipynb files.
After quite long searching, i found this article
https://stackoverflow.com/a/62657958/19118788
and in this article, below code is suggested to run ipynb file in excel VBA.
Sub execute_notebook()
Dim objShell As Object
Dim new_cmd_window As String
Dim full_script As String
Dim activate_env As String
Dim change_dir_script As String
Dim convert_and_run_nb As String
Set objShell = VBA.CreateObject("Wscript.Shell")
new_cmd_window = "cmd /c"
activate_env = "cd /d [path to Anaconda\Scripts folder which on my machine is
C:\ProgramData\Anaconda3\Scripts] & activate [environment_name] &"
change_dir_notebook = "cd /d [path to folder where notebook is] &"
convert_and_run_nb = "jupyter nbconvert --to notebook --execute [notebook name].ipynb"
full_script = new_cmd_window & " " & Chr(34) & activate_env & " " & change_dir_script & " " & convert_and_run_nb & Chr(34)
objShell.run full_script
End Sub
Most of lines are understood, but i have no idea for [environment_name] in the above code.
(I am beginner to these things)
Please tell me, what should i do for [environment_name] ??

How to make this hard-coded file path dynamic?

The code should pass "File Path" in to the File Picker dialog window and upload/attach the file. I'm consistently getting an error.
VBA Code (error at Shell and asking for Object):
Sub AttachFile()
ufile = "D:\Desktop\Movement Pass.txt"
vbsFile = "D:\Downloads\Selenium Basic\Attachment.vbs"
Shell "WScript.exe " & vbsFile & " " & ufile
End Sub
VB Script - Attachment.vbs (error at Arguments):
Set WshShell = CreateObject("WScript.Shell")
Application.wait Now + TimeSerial(0, 0, 5)
Ret = WshShell.AppActivate("Open")
WshShell.Run "cmd.exe /c echo " & Wscript.Arguments(0) & "| clip", 0, True
WshShell.SendKeys "^{v}"
With AutoIt it worked for hard-coded file paths. I want the file path dynamically (so that I can run a loop from Excel VBA). Shell command does not compile and AutoIt does not allow dynamic paths in VBA.
I am looking for a solution in VBA and Shell command. AutoIt dynamic file path in VBA can be a solution too. I know this is not the right method but this is what I know:
VBA.Shell "Explorer D:\Seleninum\VB\Get_File_Name.exe", vbNormalFocus
Paths passed as command line argument need to be quoted if they may contain spaces:
Sub AttachFile()
ufile = "D:\Desktop\Movement Pass.txt"
vbsFile = "D:\Downloads\Selenium Basic\Attachment.vbs"
Shell "WScript.exe """ & vbsFile & """ """ & ufile & """"
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?

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

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