Excel 2007+ has a new workbook property called ".HasVBProject" that you can request.
In Excel 2003 and earlier, the testing of solutions for lines of code in the CodeModule of any of the VBComponents workbooks is discussed above.
You must test the ".CountOfLines" property yourself, because the lines of code in the "Declaration" section of the code module (obtained through the ".CountOfDeclarationLines") are considered by Excel as "Macrocode" and require the preservation of macros allowed formats.
Public Function HasVBProject(Optional pWorkbook As Workbook) As Boolean ' ' Checks if the workbook contains a VBProject. ' On Error Resume Next Dim wWorkbook As Workbook Dim wVBComponent As VBIDE.VBComponent ' As Object if used with Late Binding ' Default. ' HasVBProject = False ' Use a specific workbook if specified, otherwise use current. ' If pWorkbook Is Nothing _ Then Set wWorkbook = ActiveWorkbook _ Else Set wWorkbook = pWorkbook If wWorkbook Is Nothing Then GoTo EndFunction If (VBA.CInt(Application.Version) >= 12) _ Then ' The next method only works for Excel 2007+ ' HasVBProject = wWorkbook.HasVBProject Else ' Signs the workbook has a VBProject is code in any of the VBComponents that make up this workbook. ' For Each wVBComponent In wWorkbook.VBProject.VBComponents If (wVBComponent.CodeModule.CountOfLines > 0) _ Then ' Found a sign of programmer activity. Mark and quit. ' HasVBProject = True: Exit For End If Next wVBComponent End If EndFunction: Set wVBComponent = Nothing Set wWorkbook = Nothing End Function
Dutch
Dutch source share