'********************************************************************************************************** ' Name: getDictionaryKeys ' Author: mielk | 2013-04-10 ' ' Comment: Returns set of keys of the specified dictionary as a one-dimensional array. ' ' Parameters: ' dict Dictionary which keys are to be returned. ' ' Returns: ' Variant() Array of the keys from the given dictionary. ' ' ' Exceptions: ' IllegalTypeException Thrown if the given parameter is not a dictionary. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2013-04-10 mielk Function created. '********************************************************************************************************** Public Function getDictionaryKeys(dict As Variant) As Variant() Const METHOD_NAME As String = "getDictionaryKeys" '------------------------------------------------------------------------------------------------------ Const DICTIONARY_TYPENAME As String = "Dictionary" '------------------------------------------------------------------------------------------------------ Dim varKey As Variant Dim arr() As Variant Dim lngItem As Long '------------------------------------------------------------------------------------------------------ 'Check if the given parameter dict is a dictionary. -------------------------------------------------| If VBA.StrComp(VBA.TypeName(dict), DICTIONARY_TYPENAME, vbTextCompare) Then _ GoTo IllegalTypeException '| '----------------------------------------------------------------------------------------------------| 'If the given dictionary is empty, empty array will be returned. ------------------------------------| If dict.Count Then '| '| 'Resize final table [arr] to be big enough for all the items from the given dictionary. -----| '| ReDim arr(1 To dict.Count) '| '| For Each varKey In dict.keys '| '| lngItem = lngItem + 1 '| '| '| '| 'Before adding value to the result array check if it is an object or ----------------| '| '| 'a primitive value and apply proper action. '| '| '| If VBA.IsObject(varKey) Then '| '| '| Set arr(lngItem) = varKey '| '| '| Else '| '| '| arr(lngItem) = varKey '| '| '| End If '| '| '| '------------------------------------------------------------------------------------| '| '| '| '| Next varKey '| '| '--------------------------------------------------------------------------------------------| '| '| End If '| '----------------------------------------------------------------------------------------------------| 'Assign the final table to the result variable. getDictionaryKeys = arr '========================================================================================================== ExitPoint: Exit Function '---------------------------------------------------------------------------------------------------------- IllegalTypeException: '(...) 'Error handling for the case if the given parameter is not a dictionary. GoTo ExitPoint End Function