Home » excel » excel – Accessing custom property's value gives 'Out of Memory' error when value is null

excel – Accessing custom property's value gives 'Out of Memory' error when value is null

Posted by: admin March 9, 2020 Leave a comment

Questions:

I’m trying to create a custom property in an sheet, then retrieve its value. This is fine when I don’t use an empty string, i.e. "". When I use the empty string, I get this error:

Run-time error '7':
Out of memory

Here’s the code I’m using:

Sub proptest()

Dim cprop As CustomProperty
Dim sht As Worksheet

Set sht = ThisWorkbook.Sheets("control")
sht.CustomProperties.Add "path", ""

For Each cprop In ThisWorkbook.Sheets("control").CustomProperties
    If cprop.Name = "path" Then
        Debug.Print cprop.Value
    End If
Next

End Sub

The code fails at Debug.Print cprop.value. Shouldn’t I be able to set the property to "" initially?

How to&Answers:

I think from the comments and the answer from Daniel Dusek it is clear that this cannot be done. The property should have at least 1 character to be valid, an empty string just isnt allowed and will give an error when the .Value is called.

So you Add this property with a length 1 or more string and you Delete the property again when no actual value is to be assigned to it.

Answer:

With vbNullChar it works, sample:

Sub proptest()
  Dim sht As Worksheet
  Set sht = ThisWorkbook.Sheets("control")

  ' On Error Resume Next
  sht.CustomProperties.Item(1).Delete
  ' On Error GoTo 0

  Dim pathValue As Variant
  pathValue = vbNullChar

  Dim pathCustomProperty As CustomProperty
  Set pathCustomProperty = sht.CustomProperties.Add("path", pathValue)

  Dim cprop As CustomProperty
  For Each cprop In ThisWorkbook.Sheets("control").CustomProperties
      If cprop.Name = "path" Then
          Debug.Print cprop.Value
      End If
  Next

End Sub

Answer:

As already mentioned it is not possible to set empty strings.

An easy workaround is to use a magic word or character, such as ~Empty (or whatever seems proof enough for you):

Dim MyProperty As Excel.CustomProperty = ...
Dim PropertyValue As String = If(MyProperty.Value = "~Empty", String.Empty, MyPropertyValue)

A slightly more expensive workaround but 100% safe is to start all the values of your custom properties with a character that you then always strip off. When accessing the value, systematically remove the first character:

Dim MyProperty As Excel.CustomProperty = ...
Dim PropertyValue As String = Strings.Mid(MyProperty.Value, 2)

You can write an extension to make your life easier:

<System.Runtime.CompilerServices.Extension>
Function ValueTrim(MyProperty as Excel.CustomProperty) As String
    Return Strings.Mid(MyProperty.Value, 2)
End Function

Now you can use it like this: Dim MyValue As String = MyProperty.ValueTrim

Use a reversed principle when you add a custom property:

<System.Runtime.CompilerServices.Extension>
Function AddTrim(MyProperties As Excel.CustomProperties, Name As String, Value As String) as Excel.CustomProperty
    Dim ModifiedValue As String = String.Concat("~", Value) 'Use ~ or whatever character you lie / Note Strig.Concat is the least expensive way to join two strings together.
    Dim NewProperty As Excel.CustomProperty = MyProperties.Add(Name, ModifiedValue)      
    Return NewProperty 
End Function

To use like this: MyProperties.AddTrim(Name, Value)

Hope this helps other people who come across the issue..