Check Trust Access to the VBProject


Home           Programming       Favorites      

The function below will attempt to count the VBComponents in VBPRoject.

If it fails, it likely means that the "Trust access to the VBA Project Object" security setting for macros is not enabled.

It will display a message box with the test results.
Function IsVBProjectProtected() As Boolean
' returns TRUE if the VB project is protected

   Dim VBAEditor As VBIDE.VBE
   Dim VBProj As VBIDE.VBProject
   Dim i As Integer
   Dim strResults As String

   Set VBAEditor = Application.VBE
   Set VBProj = VBAEditor.ActiveVBProject
   'VBProj.VBComponents.Count
   Dim VBC As Integer
   VBC = -1
   On Error Resume Next
   VBC = VBProj.VBComponents.Count
   On Error GoTo 0
   If VBC = -1 Then
      ProtectedVBProject = True
   Else
      ProtectedVBProject = False
   End If

End Function			

The procedure below will call the function above.
Sub callIsVBProjectProtected()

   Dim strPrompt As String

   If IsVBProjectProtected() Then
      strPrompt = "Program cannot run with"
      strPrompt = strPrompt & "current security settings"
      strPrompt = strPrompt & vbCrLf & vbCrLf
      strPrompt = strPrompt & _ 
      "You must find and Check the"
      strPrompt = strPrompt & "Trust Access to the VBA "
      strPrompt = strPrompt & _ 
      "Project Object Model checkbox."
      strPrompt = strPrompt & vbCrLf & vbCrLf
      strPrompt = strPrompt & _ 
      "Then Save, Exit and Restart Program."

      MsgBox strPrompt
   Else
      strPrompt = "Trust Access to the VBA "
      strPrompt = strPrompt & "Project Object Model "
      strPrompt = strPrompt & "is correctly enabled."

      MsgBox strPrompt

   End If
End Sub