Excel Macro to Open EXE - excel

I am trying to create a macro that will open the file "test.exe" on drive F/. I get "runtime error 5" when running this code below.
Sub MacroTangoMike()
' MacroTangoMike Macro
RetVal = Shell("F:/test.exe", 1)
End Sub
It works fine though if test.exe is on the C/ drive.

Error 5 is access denied. Are you sure you have the execute permission on the EXE on the F: drive?
Also you should probably be using backslashes \ in your paths on Windows rather than /.

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.

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

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.

Trouble Deleting files programmatically in VBA

I had a hard time with Windows 7 OS to where it would think that certain folders are in usage (not allowing folder delete), when in fact they aren't.
After alot of research and trial and error, I was able to use a command that worked well on Windows 7:
rmdir /S /Q "S:\Allied MTRS\Not Scanned\FITTINGS AND FLANGES\RG AR 2686 MOVED FOR AUTO INDEXING"
When I try to run this programmatically via shell command (see code below), it gives me: "File Not Found" message.
So if you try to run it programmatically first, it won't work. Then try to run the same thing, via command line, it works fine. Of course, if you try to run it grammatically again, after that, it will give you "File Not Found" (naturally, since the folder is already deleted). If you want to retry the experiment, you have to try on another folder....
Any ideas?
Sub tryitz()
Dim s As String
Dim ReturnCode As Integer
s = "S:\Allied MTRS\Not Scanned\FITTINGS AND FLANGES\RG AR 2686 MOVED FOR AUTO INDEXING"
s = "rmdir /S /Q " + Chr(34) + Trim(s) + Chr(34)
ReturnCode = Shell(s)
End Sub
Here is a possible answer, but I am looking for something better.
But hey, if not, at least this works, just a little bit more labor in terms of writing a bit of code...
Create a batch file
DelFile.Bat, say.
The DelFile.Bat is edited by my program (programmatically).
I edit it so that it has my desired "rmdir /S /Q? statement.
Then, I run it through shell.

Opening a image file

I am having trouble figuring out the VBA syntax to open a file for MAC OS
Here is my code
Sub OpenFile()
Open (Users/dongougkim/ShareSync/Dan's stuff/Current Services/logo.png)
End Sub
I have also tried
Open ("Users/dongougkim/ShareSync/Dan's stuff/Current Services/logo.png")
Open ("Macintosh HD/Users/dongougkim/ShareSync/Dan's stuff/Current Services/logo.png")
Open ("Macintosh HD:Users:dongougkim:ShareSync:Dan's stuff:Current Services:logo.png")
Please help. Driving me crazay
You need to prefix Users with a slash, otherwise the path is relative.
Try this:-
Sub OpenFile()
Open (/Users/dongougkim/ShareSync/Dan's stuff/Current Services/logo.png)
End Sub

How do I get rid of errors in .bat file that aren't affecting outcome of the script?

I have an application calls the below command:
C:\Users\212340141>"C:\Program Files\Microsoft Office\Office14\excel.exe" /e "C:
\My Programs\CPU Analysis\data\IOParse.xlsm" "-iodumplocation"C:\My Programs\CPU
Analysis\iodump\065901_iodump.txt""
When I run this command, it opens excel, passing in the C:\My Programs\CPU Analysis\iodump\065901_iodump.txt path as a parameter for the excel book to use in the macros it runs automatically. The macros run correctly and the file is modified correctly, but I get the following errors when I run the command:
How can I get rid of the errors?
Answer
As mentioned in the accepted answer, excel was trying to open multiple files because of how I wrote the command. The way I solved this is I created a text file to hold the path I was trying to pass into the macro. The macro would open the text file and read the path to get the path it needed. This is much cleaner and easier than trying to get the path from the command line.
Description of the startup switches for Excel lists and describes the optional switches which can be used on starting Excel. /e is listed on this page written by Microsoft. But -iodumplocation is definitely not a command line switch for Excel.
Using double quotes within a double quoted string is always a mistake on command line and the result is unpredictable as depending on code of command line parser, see answer on Why double quotes should be always only at beginning and end of an argument string?
The command line used is obviously interpreted by Excel as
"C:\Program Files\Microsoft Office\Office14\excel.exe" /e "C:\My Programs\CPU Analysis\data\IOParse.xlsm" "-iodumplocation" C:\My Programs\CPU Analysis\iodump\065901_iodump.txt""
which results in
/e ... starting without displaying the startup screen and without the creation of a new workbook.
C:\My Programs\CPU Analysis\data\IOParse.xlsm ... opening this marco-enabled workbook file.
-iodumplocation ... the failed attempt to open a file with name -iodumplocation.xlsx in current working directory.
C:\My ... the failed attempt to open a file with name My.xlsx in root of drive C:.
Programs\CPU ... the failed attempt to open a file with name CPU.xlsx in subdirectory Programs of current working directory.
Analysis\iodump\065901_iodump.txt"" ... the failed attempt to open a file with invalid name 065901_iodump.txt"" in subdirectory Analysis\iodump of current working directory.
I can't suggest a correct command line as I don't know what -iodumplocation should be.

Resources