I have a workaround for my application using CustomXMLParts rather than CustomDocumentProperties. I can probably rework my application using this in a matter of hours. This change is not backwards compatible and old versions of my spreadsheets will not work.
I have not used CustomXMLParts before, so do not know if I am abusing this feature.
thanks,
Daniel
Sub Test()
setCustomXMLPart ThisWorkbook, "Test", "Fred"
MsgBox getCustomXMLPartValue(ThisWorkbook, "Test")
setCustomXMLPart ThisWorkbook, "Test", "Sam"
MsgBox getCustomXMLPartValue(ThisWorkbook, "Test")
End Sub
Public Function GetCustomXMLPart(bk As Workbook, strPropertyName As String) As CustomXMLPart
Dim bookParts As CustomXMLParts
Dim cp As CustomXMLPart
Set bookParts = bk.CustomXMLParts
Set GetCustomXMLPart = Nothing
On Error Resume Next
For Each cp In bookParts
If cp.DocumentElement.BaseName = strPropertyName Then
Set GetCustomXMLPart = cp
Exit Function
End If
Next cp
End Function
Public Function getCustomXMLPartValue(bk As Workbook, strPropertyName As String) As Variant
Dim myPart As CustomXMLPart
Set myPart = GetCustomXMLPart(bk, strPropertyName)
If myPart Is Nothing Then Return
getCustomXMLPartValue = myPart.DocumentElement.Text
End Function
Public Sub setCustomXMLPart(bk As Workbook, strPropertyName As String, value As String)
Dim myPart As CustomXMLPart
Dim valStr As String, xmlStr As String
valStr = CStr(value)
xmlStr = "<" & strPropertyName & ">" & valStr & "</" & strPropertyName & ">"
Set myPart = GetCustomXMLPart(bk, strPropertyName)
If Not (myPart Is Nothing) Then
myPart.Delete
End If
bk.CustomXMLParts.Add (xmlStr)
End Sub