I have code that is meant to detect whether the user has enabled access to the VBA Project Object Model. (If the function returns false, I display a MsgBox to inform the user how to enable this.)
The function mainly works well. However there are scenarios where this function will return false even when the user has access enabled.
When I have this code inside an Add-In and the user opens a XLSM, Excel displays the Macros Disabled prompt. Before the user can enable, the Add-In fires the warning that they need to enable access to the Object Model!
Question 1 - How do I modify the function so that it will detect if the active workbook has macros disabled or the issue is due to the POM access not enabled?
(Note I'm usually running this in AddIns so using the hide/show sheets trick is not going to work as I didn't create the XLSM that the user opens)
I have also had this function return a false positive when the end user has their macros settings to only allow macros with signatures.
Question 2 (Bonus/Optional Question) - Is there any way the Add-In can detect if the user has this signature requirement set? (Would reading their registry keys be a reliable method?)
Function provided below (Although I'm now thinking it might be cleaner to create separate functions for the above questions and run them before checking this one)
Public Function fnblnTrustsAccessToVBAProjectObjectModel() As Boolean
Dim blnResult As Boolean
Dim VBP As Object
If Val(Application.Version) >= 10 Then
Err.Clear
On Error Resume Next
Set VBP = ThisWorkbook.VBProject
blnResult = (Err.Number = 0)
' below to get to bottom of VBA Object message firing even though it IS enabled.
' appears to happen when the XLSM does not have macros enabled and the AddIn fires
If Not blnResult Then
Debug.Print Err.Number & vbTab & Err.Description
End If
If Not TypeName(VBP) = "Nothing" Then
Set VBP = Nothing
End If
End If
On Error GoTo 0
fnblnTrustsAccessToVBAProjectObjectModel = blnResult
End Function
Bookmarks