Select to view content in your preferred language

How to check if a module exist

4223
2
06-02-2010 01:36 AM
chriss_
Emerging Contributor
Hi!
I'm programming in VBA. What I want to do is, to call a module, but only if it already exists. Is there any possibillity to check in vba if a module (Eg. "module1) exists in my project (eg. project1)?

Actually i also would be interested in the same for shapefiles. Any possibility to check if a shapefile does already exist? any ideas?

Thanks
Chris
0 Kudos
2 Replies
KirkKuykendall
Deactivated User
try this (VBE is documented somewhere at Microsoft)

Option Explicit
Sub TestModExists()
    Debug.Print ModExists("ArcID")
    Debug.Print ModExists("xxx")
End Sub

Function ModExists(name As String) As Boolean

    ModExists = False
    Dim pVBE As VBIDE.VBE
    Set pVBE = Application.VBE
    Dim l As Long
    For l = 1 To pVBE.VBProjects.Count
        Dim k As Long
        For k = 1 To pVBE.VBProjects(l).VBComponents.Count
            If pVBE.VBProjects(l).VBComponents(k).Type = vbext_ct_StdModule Then
                Dim s As String
                s = UCase(pVBE.VBProjects.Item(l).VBComponents(k).name)
                If s = UCase(name) Then
                    ModExists = True
                    Exit Function
                End If
            End If
        Next k
    Next l
End Function
0 Kudos
chriss_
Emerging Contributor
Hi Kirk!

Thank you for answering my questions again. Great

Best regards
Chris
0 Kudos