How to enter the user path and directly assign to the command prompt - excel

Right now, I need to create a VBA macro wherein I need to open the cmd.exe then, it will ask the user to enter the path of the file they need to open. Then, the path should be put in the command line. As of now, I already open the location of the cmd.exe and added an inputbox function to enter the path. But, I don't know how to put the value of the path in my command line. Sorry if this seems trivial. I am new to VBA coding.
Private Sub CommandButton22_Click()
Call Shell("cmd.exe /S /K" & "perl a.pl c:\temp", vbNormalFocus)
Dim path As String
path = InputBox("Enter a file path", "Title Here")
Open path For Output As #1
Close #1
End Sub
So, for example the user enter the path: C:\Users\aaa\Desktop\Files\
In command prompt, it should look like this:
<C:\Windows\System32>**cd C:\Users\aaa\Desktop\Files**
Is it possible to do that in VBA?
Thank you.

Related

Excel VBA Workbook_Open does not work when Excel is already open

I was trying to automate VBA run with its file opening from command line as below:
cmd.exe /C set MacroName=MyMacro \& EXCEL.EXE C:\\_documents\\Book2.xlsm
The VBA scripts and files are as follows:
Book2.xlsm/Module1
Sub MyMacro()
MsgBox "MyMacro is running..."
End Sub
Book2.xlsm/ThisWorkbook
Private Sub Workbook_Open()
Dim strMacroName As String
strMacroName = CreateObject("WScript.Shell").Environment("process").Item("MacroName")
If strMacroName <> "" Then Run strMacroName
End Sub
Now, the above cmd command does work, expectedly notifies me with the MsgBox, when Excel is not open. But it does not work when Excel is already being open with other project/file(s).
How can I make this work?
The problem was that when you launch an Excel file in the already open Excel instance passing environment variables through cmd.exe or command line does not work correctly for some reason, rather than as a VBA-side problem.

Applescript if no file is added

I have the following automator apple script that I'm using so that when I drag a file into a dock icon, it opens that file in vim:
on run {input, parameters}
set filename to POSIX path of input
set cmd to "clear && 'vim' '" & filename & "' && exit"
tell application "iTerm"
set newWindow to (create window with default profile)
tell current session of newWindow
write text cmd
end tell
end tell
end run
However, I would also like to allow clicking the icon itself to open vim without any file, i.e., running $ vim. How would I change the above script so that:
If a filename is passed, I open vim with that file, vim filename
If no filename is passed (the icon is just double-clicked), it just opens vim, with vim ?
The following example AppleScript code will do as you've asked; however, keep in mind that input is a list and as presently coded it is expecting a single item list, meaning you've only dragged and dropped one file onto the app's Dock Tile:
on run {input, parameters}
if not input is equal to {} then
set filename to POSIX path of first item of input
set cmd to "clear && 'vim' '" & filename & "' && exit"
else
set cmd to "clear && 'vim' '" & "' && exit"
end if
tell application "iTerm"
set newWindow to (create window with default profile)
tell current session of newWindow
write text cmd
end tell
end tell
end run
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

String captured from window text invalid as pathname (AutoIT)

I am running into a problem automating an installation procedure. I want to install a program, and then patch it with a custom UserPatch to remove some glitches.
The UserPatch executable must be run from the installation directory, so I must retrieve this address from the installer. Then, after the program is installed, the script must copy the UserPatch file to the installation directory, and run it from there.
I retrieve the pathname as shown below. The script copies the raw text from the window, which I strip down to just the line containing the pathname.
WinActivate("Dir browse box title")
$InstallDir = WinGetText("Dir browse box title")
$split = StringSplit( $InstallDir, "&Path", 1)
$InstallDir = $split[2]
$split = StringSplit( $InstallDir, "&OK", 1)
$InstallDir = $split[1]
ControlClick("Dir browse box title", "&Drives", "[Class:CButtonClassName; INSTANCE:2]")
Sleep(100)
When printing "$InstallDir" after the above procedure, it shows exactly what I want it to be: "C:\Program Files (x86)\Path to\Installation folder" (without '"').
The problem is this: It doesn't copy the file when I run the code snippet below. It does work when I hardcode the pathname (commented in the code below), but not when using the $InstallDir variable as retrieved by the code snippet above.
; Install the UserPatch
; $InstallDir = "C:\Program Files (x86)\Path to\Installation folder"
$UserPatchName = "SetupAoC.exe"
FileCopy($UserPatchName, $InstallDir & "\" & $UserPatchName)
Run($InstallDir & "\" & $UserPatchName)
WinWait("SetupAoC - Feature Update Tool")
ControlClick("SetupAoC - Feature Update Tool", "Install", "[Class:DirectUIHWND; INSTANCE:1]")
Send("{SPACE}")
Sleep(2000)
WinClose ("SetupAoC - Install Complete")
WinClose ("SetupAoC - Feature Update Tool")
I've got the feeling that I'm missing something obvious, but I really can't tell what the problem is? Any ideas?
Thanks in advance!
EDIT in reply to comments:
After
$InstallDir = WinGetText("Dir browse box title")
the value of $InstallDir is
&Drives
C:\ Crucial M500
&Folders
&Path
C:\Program Files (x86)\Path to\Installation folder
&OK
&Cancel
I then use StringSplit, which creates an array of strings, separated by the specified delimiter.
OK, problem eliminated.
The problem was that my "C:\path...." string was preceded and trailed by two #CRLF characters (new lines). These were also present in the raw data that I copied from the window, as I didn't include them in my delimiters.
I didn't notice them when printing the output, as they're white-space. I found out when writing the $InstallDir variable to a file, in a desperate attempt.
Anyway, Solved with
$InstallDir = StringReplace($InstallDir, #CRLF, "")
Thanks for the tips, #Matrix and #McBarby!

VBA Excel command doesn't work when called with Shell, but works when copy-pasted to cmd directly

I'm trying to run a program with an input file.
Dim command, cfx5_exe_path, cfx_file_folder, cfx_file_name As String
command = cfx5_exe_path & " -cfx " & cfx_file_folder & cfx_file_name & ".cfx "
Shell command
so it gives an error.
the resulting value of the command in the debugger is
"c:\"Program Files"\"ANSYS Inc"\v150\CFX\bin\cfx5pre.exe -cfx c:\Users\Username\Arbeit\Programming\A321_tail_flow.cfx"
If I copy-paste that into windows cmd directly and remove first/last quotation signs, then it works perfectly.
What is the problem with Shell?
The documentation says:
If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.
and it gives a small example:
Sub test()
Dim RetVal
Dim command As String
command = "C:\WINDOWS\CALC.EXE"
RetVal = Shell(command, 1)
End Sub
Here I get Error 53: file not found as calc on Windows 7 resides somewhere else. Do you get this error?
Providing the right path to calc starts the program and returns a unique ID.
However, quoting a part of the correct path will throw the error:
command = "C:\WINDOWS\""SYSTEM32""\CALC.EXE"
but quoting the full path does not:
command = """C:\WINDOWS\SYSTEM32\CALC.EXE"""
So you must remove all embedded quotes and then quote the full path once.

Shell command on Excel with long path name don't work

I have a batch file run.bat in a network folder "L:\Common Data\myfile" and i want to execute it from an Excel's macro.
Googling around I found these sintax:
Call Shell(Environ$("COMSPEC") & " /k L:\Common Data\myfile\run.bat", vbNormalFocus)
but it fails because it reads only "L:\Common".
I tryed many suggestion found on Internet but no one succeeded.
Someone have a solution?
Path names with spaces have to be wrapped in quotes.
Call Shell(Environ$("COMSPEC") & " /k ""L:\Common Data\myfile\run.bat""", vbNormalFocus)

Resources