Excel VBA Workbook_Open does not work when Excel is already open - excel

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.

Related

Calling powershell or ruby script by VBA, ideally take output to Excel or point user to output path

I need Excel VBA to call another script language using shell. Ideally, also returning its output, but if that is hard to do, just to execute that other script and tell the user in a pop-up MessageBox where to get the output from and paste it into the Excel file.
I tried several ways, including (I took out some full file names):
pscmd = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File ......\.....ps1"
or
Shell("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe ""........ps1""", vbNormalFocus) - with full path to powershell or not
or
Shell("ruby.exe ""C:\testsoft\test.rb", "C:\testsoft\tst.txt""", vbNormalFocus)
or
Shell("powershell -ExecutionPolicy Bypass "".....ps1 -input """ & pathVariable & """", 1)
or
Sub RunPowershellScript()
strCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File ""C:\testsoft\mdruby-nolog.ps1"""
Set wshShell = CreateObject("WScript.Shell")
Set WshShellExec = wshShell.Exec(strCommand)
strOutput = WshShellExec.StdOut.ReadAll
MsgBox strOutput
End Sub
but all to no avail.
It does not work at all with running a ruby script or powershell script or opening cmd.
For example, calling up some programs that I tested work though:
strCommand = "ping.exe 127.0.0.1"
strCommand = "notepad.exe" ' works even with specifying a file to open
strCommand = "calc.exe"
My powershell or ruby scripts shall calculate MD5 checksums of PDFs created from Excel using VBA. They work on their own, just fine.
Is there an easy way?

Vba Shell call works with some programs but does not work with others

Shell call works with some programs that I have but does not work with others. I am putting the most basic call here for opening an exe without any further commands
Shell "C:\Program Files\qBittorrent\qbittorrent.exe", 1 'works
Shell "C:\Program Files\AmiBroker\Broker.exe", 1 'works
Shell "C:\Program Files\CTrading\QuantShare\QuantShare.exe", 1 'Run-time error '5': Invalid procedure call or argument
Shell "C:\Program Files (x86)\Equis\MetaStock\MsWin.exe", 1 'same error
I am on the verge of giving up unless I meet a savior here!!
System: Windows 10 Pro Version 1909 OS Build 18363.476
Microsoft Excel 2013 64-bit
A bit of an ugly fix, but it might help:
Shell "cmd /c ""C:\Program Files\CTrading\QuantShare\QuantShare.exe""", 1
I don't have any of the programs you are having problems with, so it is a bit of a guess. If this doesn't help, the next step is cmd /c start ...
Finally, i could use ShellExecute as follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A2000")) Is Nothing Then
Set objShell = CreateObject("Shell.Application")
para = "TaskManager ChangeSymbol newsymbol:" & ActiveCell.Value
objShell.ShellExecute "C:\Program Files\CTrading\QuantShare\QuantShare.exe", para, "", "open", 1
Set objShell = Nothing
Cancel = True
End If
End Sub
Can I make ShellExecute not to open a new instance of the program and rather activate the currently open instance and pass the arguments (similar to Shell call? Also how to make ShellExecute not take focus? I want the focus to stay back in Excel.

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.

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

Excel Macro to Open EXE

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 /.

Resources