Yesterday i was a-Googling (there are other search engines) and came across a very useful page; http://www.datapigtechnologies.com/d...umerations.txt
that lists MS enumerations, e.g. (a short one):
Public Enum XlApplyNamesOrder
xlColumnThenRow = 2
xlRowThenColumn = 1
End Enum
Now one can readily get the value usually hidden by a name. But what if you want the name for a value as I do in my reporter tools? Easy! Place the data in a module (name it XLEnumertions), add a macro to fetch the result and it's done. Here's the code:
'===============================================
' Find the reserved word naming the value passed
'===============================================
'
Function DeEnumerateMS(myEnum As String, myValue As Integer, myName As String) As Boolean
' Requires: Enum name, value. Returns reserved name (myName does not need to be initialised before call)
' Return true and reserved name if found, return False if not found
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim SL As Long ' start line
Dim SL2 As Long ' end enum line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim S As String, E As String
Set VBProj = ThisWorkbook.VBProject ' I'm reading my own source!
Set VBComp = VBProj.VBComponents("XLEnumertions") ' this module name
Set CodeMod = VBComp.CodeModule
With CodeMod
SL = 4 ' start at line 4 after the credits
EL = .CountOfLines ' search initially the whole module
SC = 1
EC = 15
' find the Enum name
If Not .Find(Target:=myEnum, _
StartLine:=SL, _
EndLine:=CodeMod.CountOfLines, _
StartColumn:=13, _
EndColumn:=EC, _
wholeword:=True, MatchCase:=False, patternsearch:=False) Then
myName = "Enum" ' did not finde Enum specified
Exit Function
End If
' locate the End Enum line (must be one)
SL = SL + 1
SL2 = SL
B = .Find(Target:="End Enum", _
StartLine:=SL2, _
EndLine:=CodeMod.CountOfLines, _
StartColumn:=1, _
EndColumn:=8, _
wholeword:=True, MatchCase:=False, patternsearch:=False)
' locate the value within the Enum bounds just identified
E = "= " & CStr(myValue)
For SL = SL To SL2
S = CodeMod.Lines(SL, 1)
x = InStrRev(S, E)
If x > 0 Then
' return success directly
myName = Mid(S, 5, x - 6)
DeEnumerateMS = True
Exit Function
End If
Next
' not found so fail on enum value
myName = "Value" ' Enum OK but value not found
End With
End Function
I'm indebted to http://www.cpearson.com/Excel/VBE.aspx for the method in the code.
This macro assumes these things:- You've reduce the headings at teh top of the module from Datapig to fewer than 4 lines (put comments at the foot of the module after the functin).
- 'Trust access to the VBA Project Object model' must be true to allow reading the source directly.
- VBA extensibility 5.3 must be referenced in Tools
Then place the module in your personal workbook, or perhaps an add-in. It is then available to other macros you write. Add project references if needed.
One odd thing is that the code does read its own module code to get the definitions, thereby not duplicating that data anywhere.
Bookmarks