How to add a DocumentProperty to CustomDocumentProperties in Excel?

calendar_today Asked Feb 13, 2013
thumb_up 6 upvotes
history Updated April 16, 2026

Question posted 2013 · +5 upvotes

I’m trying to add a DocumentProperty to the CustomDocumentProperties collection. Code as follows:

Sub testcustdocprop()
Dim docprops As DocumentProperties
Dim docprop As DocumentProperty

Set docprops = ThisWorkbook.CustomDocumentProperties
Set docprop = docprops.Add(Name:="test", LinkToContent:=False, Value:="xyz")

End Sub

Running this gives me the following error:

Run-time error '5':
Invalid procedure call or argument

I tried running it with .Add as a void function, like so:

docprops.Add Name:="test", LinkToContent:=False, Value:="xyz"

This gave me the same error. How do I add a custom document property?

Accepted answer +6 upvotes

Try this routine:

Public Sub subUpdateCustomDocumentProperty(strPropertyName As String, _
    varValue As Variant, docType As Office.MsoDocProperties)

    On Error Resume Next
    Wb.CustomDocumentProperties(strPropertyName).Value _
        = varValue
    If Err.Number > 0 Then
        Wb.CustomDocumentProperties.Add _
            Name:=strPropertyName, _
            LinkToContent:=False, _
            Type:=docType, _
            Value:=varValue
    End If
End Sub

Top excel-vba Q&A (6)

+6 upvotes ranks this answer #114 out of 136 excel-vba solutions on this site .