VBA: Check if a workbook has macros without having the "Trust access to VBA project" option checked?

ou81aswell

Board Regular
Joined
Oct 6, 2009
Messages
50
Is there a way to determine programatically if a workbook contains macros / VBA without having the "Trust access to VBA project" option checked?

Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Thanks but I'm not trying to determine if macros are enabled. Rather, I'm trying to determine if a workbook has macros. The simple way of doing this would appear to be using the VBProject object but if "Trust access to VBA Project" is not checked, I can't use this method.
 
Upvote 0
Here's an untested function that returns TRUE if a workbook has VBA modules and FALSE if not.
Code:
Function TestForVBComponents(wbkName As String) As Boolean
Dim VBCodeMod As Object
Dim ctAll As Long, ctMods As Long, ctShts As Long

Application.Volatile

ctAll = Workbooks(wbkName).VBProject.VBComponents.Count
ctShts = Workbooks(wbkName).Sheets.Count
ctMods = ctAll - ctShts - 1  '-1 for ThisWorkbook component
If ctMods > 0 Then
    TestForVBComponents = True
Else
    TestForVBComponents = False
End If

End Function
 
Upvote 0
Thanks but I think I tried that approach but wasn't able to access the VBProject object because of the security setting. I'll double check.
 
Upvote 0
As I suspected, you can't access the VBProject object if the trust access to VBA Project option is not set.

I'm going to take a different approach.

Thanks guys.
 
Upvote 0
This seems to work.

Code:
Public Sub test99()
On Error GoTo Catch
MsgBox ThisWorkbook.VBProject.VBComponents.Count
Exit Sub
Catch:
MsgBox "Hey"

End Sub
 
Upvote 0
Oh never mind, I missed the point of this thread. I was too busy looking to see whether I could check whether a project was trusted.

Now this leaves me wondering about the ultimate solution to this question.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top