Helping out in a forum, the question was
How can I determine if Excel is installed on the computer or not?
One option would be to attempt to use CreateObject() and trap the 429 error, something like:
Function IsInstalled() As Boolean
On Error GoTo Error_Handler
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
' Cleanup, Close, ...
IsInstalled = True
Error_Handler_Exit:
On Error Resume Next
If Not oApp Is Nothing Then Set oApp = Nothing
Exit Function
Error_Handler:
If Err.Number <> 429 Then 'ActiveX component can't create object, So not installed
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: IsInstalled" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
End If
Resume Error_Handler_Exit
End Function
You can simply replace Excel, with any other program: Word, InfoPath, …
But I didn’t like the above too much, so I remembered I had to code to check the installation path of any program and thought to myself that it could easily be adapted to suit this purpose as well. So with that in mind, I was able to create:
'IsAppInstalled("msaccess.exe")
'IsAppInstalled("excel.exe")
'IsAppInstalled("infopath.exe")
'IsAppInstalled("outlook.exe")
'IsAppInstalled("powerpnt.exe")
'IsAppInstalled("winword.exe")
Function IsAppInstalled(sApp As String) As Boolean
On Error GoTo Error_Handler
If Len(CreateObject("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" & sApp & "\")) > 0 Then IsAppInstalled = True
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
If Err.Number <> -2147024894 Then 'Invalid root in registry key, not installed!
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: IsAppInstalled" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Debug.Print Err.Number, Err.Description
End If
Resume Error_Handler_Exit
End Function
So with a single line of code, you can easily determine if a program is installed.
Daniel –
In place of “If Err.Number 429 ”
do you mean “If Err.Number = 429 ”
?
In this case I want to ignore the error so the function returns False, so no, I do not want to use an equality here. Things could be written differently to utilize an equality, it a coding style I guess.
I am running Windows 10, MS Office 2019 and have been using similar code as shown above since I had a Windows 7, MS Office 2010 system. I am checking for Word and it always ran fine but at some point in 2022 it began to generate a Macro security message: Microsoft Office has identified a potential security concern. This does not occur when I use the above code for any other Office program, Excel, Outlook etc., just Word. Any thoughts? Thank you.
Also, I get the same Macro warning now when I do actually open a word document from VBA: Set objWord = CreateObject(“Word.Application”). It still will open the document and fills it in, just annoying macro message.