'********************************************************************************************************** ' Name: isNonEmptyString ' Author: mielk | 2014-09-20 ' ' Description: Function to check if the given value is a non-empty string. ' Note that strings consisting only of blank characters (i.e. spaces) are also ' considered to be an empty string. ' ' Parameters: ' value Value to be checked. ' ' ' Returns: ' Boolean True is returned only if the given value or its text representation is a non-empty ' string. ' False value is returned in a few cases: ' * the source parameter [value] is an empty string, ' * the source parameter [value] is a string consisting only of blank characters, ' * the source parameter [value] is not a string and cannot be converted into ' string (i.e it is object or array). ' ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2014-09-20 mielk Function created. '********************************************************************************************************** Public Function isNonEmptyString(ByVal value As Variant) As Boolean Const METHOD_NAME As String = "isNonEmptyString" '------------------------------------------------------------------------------------------------------ If Not VBA.IsObject(value) Then isNonEmptyString = VBA.Len(removeSpaces(stringify(value))) > 0 End If End Function '********************************************************************************************************** ' Name: removeSpaces ' Author: mielk | 2012-11-18 ' ' Comment: Function to remove all the blank characters from the given string. ' ' Parameters: ' text String to be cleaned of blank characters. ' The given value must be of a String type or of any other type that can be ' converted to String. ' The following characters are considered by this function as blank: ' ' Character | ASCII code ' ------------------------|-------------- ' Horizontal Tab | 9 ' Line Feed | 10 ' Carriage Return | 13 ' Space | 32 ' No-Break Space | 160 ' ' Returns: ' String The original text cleaned of blank characters. ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2012-11-18 mielk Function created. '********************************************************************************************************** Public Function removeSpaces(ByVal text As String) As String Const METHOD_NAME As String = "removeSpaces" '------------------------------------------------------------------------------------------------------ removeSpaces = VBA.Replace(text, VBA.Chr$(9), "") '..........................tab removeSpaces = VBA.Replace(removeSpaces, VBA.Chr$(10), "") '.................... linefeed removeSpaces = VBA.Replace(removeSpaces, VBA.Chr$(13), "") '.................... carriage removeSpaces = VBA.Replace(removeSpaces, VBA.Chr$(32), "") '....................... space removeSpaces = VBA.Replace(removeSpaces, VBA.Chr$(160), "") '....................... space End Function '********************************************************************************************************** ' Name: stringify ' Author: mielk | 2014-09-20 ' ' Description: Function to convert the given parameter into String. ' This function works similar to VBA built-in function VBA.CStr but it doesn't throw ' an error if value passed as a input parameter cannot be converted to String. ' ' ' Parameters: ' value Value to be converted into String. ' ' ' Returns: ' String The string representation of the given parameter. ' * For primitives value, function returns the same value as VBA built-in function ' VBA.CStr. ' * For arrays, function returns all its values converted to String. ' * For object, function check if it contains function toString and returns its ' result. If there is no such method for this object, value defined in OBJECT_TAG ' constant is returned. ' ' ' ' --- Changes log ----------------------------------------------------------------------------------------- ' 2014-09-20 mielk Function created. '********************************************************************************************************** Public Function stringify(value As Variant) As String Const METHOD_NAME As String = "stringify" '------------------------------------------------------------------------------------------------------ Const OBJECT_TAG = "[Object]" '------------------------------------------------------------------------------------------------------ On Error Resume Next 'For missing and empty parameters, empty String is returned. ----------------------------------------| If Not VBA.IsMissing(value) And Not VBA.isEmpty(value) Then '| '| 'There is different logic for obtaining String representation of object and -----------------| '| 'primitive value, so function needs to check which one is the given parameter. '| '| If VBA.IsObject(value) Then '| '| stringify = value.toString '| '| If VBA.Len(stringify) = 0 Then stringify = OBJECT_TAG '| '| Else '| '| stringify = VBA.CStr(value) '| '| End If '| '| '--------------------------------------------------------------------------------------------| '| '| End If '| '----------------------------------------------------------------------------------------------------| End Function