Ever wanted a function to validate whether a Module existed in the current VBA project?
Here it is!
'---------------------------------------------------------------------------------------
' Procedure : VBE_ModExist
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Determine if a module exists or not
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
' Compatibility: This is not application specific, so it should work in any VBA project.
' (tested in Word, Excel & Access)
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sModuleName : Name of the Module to validate the existance of
'
' Usage:
' ~~~~~~
' ? VBE_ModExist("Form_Form1")
' ? VBE_ModExist("Module1")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2019-01-02 Initial Release
'---------------------------------------------------------------------------------------
Public Function VBE_ModExist(ByVal sModuleName As String) As Boolean
Dim oVBComp As Object
On Error Resume Next
Set oVBComp = Application.VBE.ActiveVBProject.VBComponents(sModuleName)
VBE_ModExist = (Err.Number = 0)
If Not oVBComp Is Nothing Then Set oVBComp = Nothing
End Function
Important
Do pay special attention when validating Forms and Reports as you need to include the Form_ or Report_ prefix and the name of the object.