我试图找到其他方法来检测应用程序,而不依赖于 CreateObject 中的错误
这使用了 WMI 对象,它似乎工作得很好,但它不区分演示版本
它列出了注册表路径Microsoft\Windows\CurrentVersion\App Paths(32 位和 64 位)中已安装的应用程序
Public Function AppDetected() As Boolean
Const HKEY_LOCAL_MACHINE = &H80000002 'HKEY_CURRENT_USER = &H80000001
Const APP_PATH = "\Microsoft\Windows\CurrentVersion\App Paths\"
Const APP_PATH_32 = "SOFTWARE" & APP_PATH
Const APP_PATH_64 = "SOFTWARE\Wow6432Node" & APP_PATH
Const REG_ITM = "!\\.\root\default:StdRegProv"
Const REG = "winmgmts:{impersonationLevel=impersonate}" & REG_ITM
Const ID = "Outlook" '"OUTLOOK.EXE"
Dim wmi As Object, subKeys As Variant, found As Variant
If wmi Is Nothing Then Set wmi = GetObject(REG)
If wmi.EnumKey(HKEY_LOCAL_MACHINE, APP_PATH_32, subKeys) = 0 Then
If Not IsNull(subKeys) Then found = UBound(Split(Join(subKeys), ID)) > 0
End If
If Not found Then
If wmi.EnumKey(HKEY_LOCAL_MACHINE, APP_PATH_64, subKeys) = 0 Then
If Not IsNull(subKeys) Then found = UBound(Split(Join(subKeys), ID)) > 0
End If
End If
AppDetected = found
End Function
注意:我只在没有 Outlook 的机器上测试过
来自 MS 的有关 WMI Tasks: Registry 的更多详细信息
另一个使用 MIME 的 WMI 版本,在 VBScript 中显示已安装的 MS 应用程序:
Set wmi = GetObject("winmgmts:\\.\root\CIMV2")
Set itms = wmi.ExecQuery("SELECT * FROM Win32_MIMEInfoAction", "WQL", &h10 + &h20)
For Each itm In itms
WScript.Echo itm.Name
Next
检测MS Mail,类似于CreateObject:Application.ActivateMicrosoftApp xlMicrosoftMail
确定 Outlook 用户帐户:
'If Outlook exists, set reference to Microsoft Outlook *
Public Function ShowOutlookAccount() As Long
Dim appOutlook As Outlook.Application, i As Long
Set appOutlook = CreateObject("Outlook.Application")
For i = 1 To appOutlook.Session.Accounts.Count
Debug.Print appOutlook.Session.Accounts.Item(i) & " : Account number " & i
Next
End Function
来自 Ron de Bruin 的更多 Outlook utils