VBA Shell命令中的Explorer.exe

时间:2016-10-21 01:39:01

标签: vba

我在VBA中遇到过使用Shell的两种变体:

Call Shell("Explorer.exe ""C:\Windows\system32\notepad.exe""",vbNormalFocus)Call Shell("C:\Windows\system32\notepad.exe",vbNormalFocus)。 它们都有效,所以我倾向于总是使用第二个例子,它更简单,更直接。也许我错过了一些东西,但有没有理由可以使用第一个例子?

1 个答案:

答案 0 :(得分:0)

这将解释,它打开2个记事本实例,第1个是资源管理器方式,第2个是记事本,然后代码的后半部分查看记事本和资源管理器的实例,并显示相应的进程ID。可以看出,Explorer打开的版本作为一个进程耦合到Windows资源管理器,而第二个记事本作为一个进程

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

我的结果

Via Explorer : 9932 - Via Notepad : 3776

我的流程

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 

希望这有帮助。

相关问题