Enum SecurityLevel
IllegalEntry
SecurityLevel1
SecurityLevel2
End Enum
Public Sub Test()
Dim E As Long
Call AddReferenceVBA
For E = SecurityLevel.IllegalEntry To SecurityLevel.SecurityLevel2
MsgBox StrEnumVal("SecurityLevel", E)
Next
End Sub
Public Function StrEnumVal(EnumName As String, EnumItm As Long) As String
' Reference "Microsoft Visual Basic for Applications Extensibility 5.3" must be installed
'Call AddReferenceVBA
On Error Resume Next
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim CodeMac As VBIDE.CodePane
Dim numLines As Long ' end line
Dim lineNum As Long
Dim thisLine As String
Dim message As String
Dim numFound As Long
Dim IsEnum As Boolean
Dim Itm As Long
Set VBProj = ThisWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
With CodeMod
numLines = .CountOfLines
Fnd = "Enum " & Fnd
Itm = numLines
For lineNum = 1 To numLines
thisLine = .Lines(lineNum, 1)
If InStr(1, thisLine, Fnd, vbTextCompare) > 0 Then
IsEnum = True
End If
If IsEnum = True Then
If InStr(1, thisLine, "End Enum", vbTextCompare) > 0 Then Exit For
If InStr(thisLine, ":") > 0 Then
For s = 0 To UBound(Split(thisLine, ": "))
EnumItems = EnumItems & IIf(EnumItems <> "", vbNewLine, "") & Split(Split(thisLine, ": ")(s), " = ")(0)
Next
Else
EnumItems = EnumItems & IIf(EnumItems <> "", vbNewLine, "") & Split(thisLine, " = ")(0)
Itm = Itm + 1
End If
End If
Next lineNum
End With
If InStr(EnumItems, Fnd & vbNewLine) > 0 Then
EnumItems = Replace(EnumItems, Fnd & vbNewLine, "")
Else
EnumItems = Replace(EnumItems, Fnd, "")
End If
Fnd = ""
EnumItems = Replace(EnumItems, " ", "")
StrEnumVal = Split(EnumItems, vbNewLine)(EnumItm)
End Function
Sub AddReferenceVBA()
AddRef ThisWorkbook, "{0002E157-0000-0000-C000-000000000046}", "VBIDE", 5, 3
End Sub
Sub AddRef(wbk As Workbook, sGuid As String, sRefName As String, sRefMajor As Long, sRefMinor As Long)
Dim i As Integer
On Error GoTo EH
With wbk.VBProject.References
For i = 1 To .Count
If .Item(i).Name = sRefName Then
Exit For
End If
Next i
If i > .Count Then
.AddFromGuid sGuid, sRefMajor, sRefMinor ' 0,0 should pick the latest version installed on the computer
ThisWorkbook.Save
End If
End With
EX: Exit Sub
EH: MsgBox "Error in 'AddRef'" & vbCrLf & vbCrLf & Err.Description
Resume EX
Resume ' debug code
End Sub