0

I've come across two variations of using Shell from VBA:

Call Shell("Explorer.exe ""C:\Windows\system32\notepad.exe""",vbNormalFocus) and Call Shell("C:\Windows\system32\notepad.exe",vbNormalFocus). They both work, so I would tend to always use the 2nd example, it's simpler and more straightforward. Maybe I'm missing something, but is there any reason why one might use the 1st example?

DaveU
  • 1,082
  • 2
  • 14
  • 25
  • It's Explorer.exe a file manager application but not a web browser. – Mak Oct 21 '16 at 02:47
  • That didn't answer my question. – DaveU Oct 21 '16 at 03:33
  • The 1st opens the Windows Explorer then opens the file, as if putting the full path of a file in the browser. The 2nd opens notepad, to be honest Shell help should of explained, try shell("Explorer.exe") and then something like this shell("Notepad.exe ""C:\TEST\JUNK\test.txt""") opens the file test.txt, the 2nd part is the argument of the shell, so in this case what file to open. Not 100%, but I think it's just been done for best practice, to tell the opening of the application to be done via windows explorer. – Nathan_Sav Oct 21 '16 at 07:28
  • So the 1st one will be dealt with Windows Explorer and the 2nd Notepad in any returns, back from the shell maybe? – Nathan_Sav Oct 21 '16 at 07:31
  • From the answers I'm getting (which I appreciate) it looks like maybe I wasn't stating the issue clearly, so I've re-worded the question. – DaveU Oct 21 '16 at 16:38
  • I found the answer I was looking for [here](http://stackoverflow.com/a/39903527/2023624) – DaveU Oct 23 '16 at 19:18
  • I know it's old but I would like to add my two cents due to a recent discovery. I created a vba code that would in turn, create a `.bat` script, populate the code and then run it. My issue was anytime I used `Shell` to run it, Malewarebytes (that my company uses) was shutting the process down as it was detecting it as malware. I couldn't figure out how to get round it without shutting off some exploit protection which I couldn't do so I thought to just auto-open an explorer browser to the the file and the user could manually run it from there... – Simon Nov 24 '20 at 20:20
  • ...Luckily when I ran the code to what I thought would open explorer to the file, it actually ran the file and Malewarebytes didn't stop it. So for me, running something that an Antivirus software might try to stop, seems to work running it through explorer. Note: Adding an exception to the file or even cmd.exe didn't work. Only removing protection from Excel or turning it off prevented it from being blocked. – Simon Nov 24 '20 at 20:22

1 Answers1

0

This will explain, it opens 2 instances of notepad, the 1st the explorer way, the 2nd just notepad, then the 2nd half of the code looks at the instances of notepad and explorer and shows the corresponding process ID's. It can be seen that the Explorer opened version is coupled to Windows Explorer as a process, and the 2nd Notepad as a process

Sub Shell_Example()

Dim o1 As Long
Dim o2 As Long

'From Shell Help
'Runs an executable program and returns a Variant (Double) representing the
'program's task ID if successful, otherwise it returns zero.

o1 = Shell("Explorer.exe ""C:\Windows\system32\notepad.exe""", vbNormalFocus)
o2 = Shell("Notepad.exe")

Debug.Print "Via Explorer : " & o1 & " - Via Notepad : " & o2

Dim objServices As Object, objProcessSet As Object, Process As Object

Set objServices = GetObject("winmgmts:\\" _
        & "." & "\root\CIMV2")
Set objProcessSet = objServices.ExecQuery _
        ("Select Name, ProcessID FROM Win32_Process", , 48)

For Each Process In objProcessSet
    If Process.Name Like "*notepad.exe" Or Process.Name Like "*explorer.exe" Then _
        Debug.Print Process.Name, Process.ProcessID
Next

End Sub

My Results

Via Explorer : 9932 - Via Notepad : 3776

My Processes

explorer.exe   5240 
notepad.exe    4820 
notepad.exe    7092 
notepad.exe    1492 
notepad.exe    11296 
explorer.exe   2616 
notepad.exe    1276 
**explorer.exe   9932** 
**notepad.exe    3776** 
explorer.exe   11552 

Hope this helps.

Nathan_Sav
  • 8,466
  • 2
  • 13
  • 20
  • Thanks for the reply re the processes. But I guess my question still remains - say all I want to do is launch Notepad from within Excel, why would I ever use use the `Shell("Explorer.exe, etc` syntax. Seems quite unnecessary. – DaveU Oct 21 '16 at 16:29
  • Even though your answer wasn't quite what I was looking for (see my comment above), I'm going to accept it, as you did pass along some very useful information. – DaveU Oct 23 '16 at 19:25
  • What are you looking for then? My answer explains the difference in what's happening, one opens notepad through explorer, as if you double clicked Notepad.exe in explorer, so you'll have a explorer.exe process and a notepad process, you'll not know the notepad process from shell, the other opens notepad.exe in the shell, so you'll have the process id. – Nathan_Sav Oct 24 '16 at 05:49
  • Sorry, `(see my comment above)` was referring to a comment I added to my original post, and contains a [link](http://stackoverflow.com/a/39903527/2023624). There I found the simple answer I was looking for. But thanks for your reply, you did give me some new information. – DaveU Oct 24 '16 at 06:31