如前所述,无法设置空字符串。
一个简单的解决方法是使用一个神奇的词或字符,例如~Empty
(或任何对你来说足够证明的东西):
Dim MyProperty As Excel.CustomProperty = ...
Dim PropertyValue As String = If(MyProperty.Value = "~Empty", String.Empty, MyPropertyValue)
一个稍微昂贵但 100% 安全的解决方法是使用一个字符开始自定义属性的所有值,然后您总是将其剥离。访问值时,系统地删除第一个字符:
Dim MyProperty As Excel.CustomProperty = ...
Dim PropertyValue As String = Strings.Mid(MyProperty.Value, 2)
你可以编写一个扩展来让你的生活更轻松:
<System.Runtime.CompilerServices.Extension>
Function ValueTrim(MyProperty as Excel.CustomProperty) As String
Return Strings.Mid(MyProperty.Value, 2)
End Function
现在你可以像这样使用它:Dim MyValue As String = MyProperty.ValueTrim
添加自定义属性时使用相反的原则:
<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
像这样使用:MyProperties.AddTrim(Name, Value)
希望这可以帮助遇到这个问题的其他人..