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)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.