How to make this hard-coded file path dynamic? - excel

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

Related

VBA: Shell() command to keep CMD after execution completes

I am executing a .py python script from VBA using the Shell()
Sub Run_Python()
Dim PYTHON_FULL_PATH, PYTHON_SCRIPT_PATH As String
iRowNo = 2
Do Until Sheet7.Cells(iRowNo, 1) = ""
PYTHON_FULL_PATH = Sheet7.Cells(iRowNo, 1)
PYTHON_SCRIPT_PATH = Sheet7.Cells(iRowNo, 2)
iRowNo = iRowNo + 1
Loop
RetVal = Shell(PYTHON_FULL_PATH & " " & PYTHON_SCRIPT_PATH, vbNormalFocus)
End Sub
The Python script prints few lines as part of the code and those print statements are shown in the CMD prompt.
But once the execution completes or errored our the CMD gets closed automatically.
Is there a way to keep the CMD prompt visible even after the execution gets completed ?
Thanks,
Run cmd.exe with /k parameter:
RetVal = Shell("cmd /k " & PYTHON_FULL_PATH & " " & PYTHON_SCRIPT_PATH, vbNormalFocus)

VBA SHELL COMMAND not executing

I am novice to VBA shell. So i request an explanation to understand how things work.
Through EXCEL VBA, I would like to create a *.bat file and execute the same automatically. However my command lines ONLY creates the file and does not execute it. Please suggest.
Sub Create_Motor_Model()
Application.ScreenUpdating = False
CURRENT_SHEET = "Script"
Output_path = ActiveWorkbook.Path
InputData1 = Cells(1, 2)
file_output = Output_path & "\" & "Input.bat"
Open file_output For Output As #1
Print #1, Chr(34) & InputData1 & Chr(34) & " -b -i " & CURRENT_SHEET & ".mac -o Output.out"
Close #1
Shell ActiveWorkbook.Path & "\Input.bat"
End Sub

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?

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