' needs reference to VBE extension
' use like
' dim XModule:set xModule = GetMakeComp("MyNewMod",2)
' vbext_ComponentType 1 std 2 class 3 form 11 activeX 100 Doc
'Function GetMakeComp(ModNa$, Typ As vbext_ComponentType ) As CodeModule
Function GetMakeComp(ModNa$, Optional Typ As vbext_ComponentType = 2) As CodeModule
Dim Li&, Foundit As Boolean
While Not Foundit And Li < ActiveWorkbook.VBProject.VBComponents.Count
Li = Li + 1
Foundit = ActiveWorkbook.VBProject.VBComponents.Item(Li).Name = ModNa
Wend
' the exit and OnError methods do Have to look through all items until Found
If Foundit Then
If ActiveWorkbook.VBProject.VBComponents.Item(Li).Type = Typ Then
Set GetMakeComp = ActiveWorkbook.VBProject.VBComponents.Item(Li).CodeModule
Else
MsgBox " A Component with Name " & ModNa & " Exists but is of wrong type"
End If
Else
With ActiveWorkbook.VBProject.VBComponents.Add(Typ)
.Name = ModNa
Set GetMakeComp = .CodeModule
End With
End If
End Function