I currently have a code that on startup checks if the option "Trust access to the VBA project object model" is enabled or not.
In the case that it is not enabled, I need the program to open the macro security settings for easy access for the user.
I've made some code that does this in most cases, but I've encountered one problem that I don't know how to get around.
The code is as follows:
Private Sub Workbook_Open
If Not VBATrusted() Then
MsgBox "Trust access to the VBA project object model is not enabled" & vbNewLine & vbNewLine & _
"Please allow access by ticking the checkbox in the window that appears after clicking Ok"
Application.CommandBars.ExecuteMso ("MacroSecurity")
End If
End Sub
Function VBATrusted() As Boolean
On Error Resume Next
VBATrusted = (Application.VBE.VBProjects.Count) > 0
End Function
This code does its job unless the macro settings are as default "Disable all macros with notification", in which case I activate the macro and then get a run-time error "-2147467259 (80004005) Method 'ExecuteMso' of object '_CommandBars' failed"
This error will only occur on the first startup, as I do not have to activate the macros on consecutive startups, unless I move the file location.
I've tried pausing the macro for two seconds, but that did nothing for me, and neither did an error handler that tried to grab the error and then try executing the Application.CommandBars.ExecuteMso ("MacroSecurity") line again. It ended up with the same error.
The debugger tells me that the error is in the Application.CommandBars.ExecuteMso ("MacroSecurity") line, but that's probably not much of a surprise with that error message.
Application.OnTimeevent that fires in 2 seconds? - CLR