How to programmatically change custom document properties in Word – and and example of why you should not trust Microsoft help files!
I have received a number of messages from readers (Word guru George Mair was the first) informing me that I was mistaken in saying the Word custom document properties collection was read-only. Well, for once I am very glad to be wrong, since this opens up a number of possibilities that I had thought were blocked! Here’s how this happened (hopefully somebody else will find this instructive, bearing in mind the condition of Office Help, which unfortunately has become less helpful with each recent version):
I wanted to add some custom Word document properties from VBA code, so I tried something like
Set prps = objWord.ActiveDocument.CustomDocumentPropertiesstrProp = “TestProp2”
prps.Add strProp
I got an error, so I looked up the CustomDocumentProperties collection in Word Help, and found this statement:
Returns a DocumentProperties collection that represents all the custom document properties for the specified document. Read-only.
Well, I figured, that was that. The collection was read-only, and that’s why I got an error. I should have looked further. That very same Help topic which said the collection was read-only had an example of using the Add method to create a new custom document property! And if you search in a different way, you get another Help topic which says
Use the Add method to create a new custom property and add it to the DocumentProperties collection.
All of this illustrates how Office Help often contains inaccurate information, and sometimes has examples with long-outdated or non-functional code. If Help says something is possible, it may not be possible; or viceversa. If you want to do something, and Help says it can’t be done, ask around – somebody may know how to do it!
So, here is some code that creates a Word custom document property:
Public Function AddDocPropsTest()
‘Written by Helen Feddema 3-1-99
‘Last modified 6-27-99
On Error GoTo AddDocPropsTestError
Dim strWordTemplate As String
Dim doc As Word.Document
Dim objWord As Word.Application
Dim objDocs As Word.Documents
Dim prps As Object
Dim strProp As String
Set objWord = CreateObject(“Word.Application”)
Set objDocs = objWord.Documents
strWordTemplate = “D:TemplatesHBF Normal.dot”
objDocs.Open strWordTemplate
Set prps = objWord.ActiveDocument.CustomDocumentProperties
strProp = “TestProp2”
Debug.Print “Adding doc property: ” & strProp
prps.Add Name:=strProp, LinkToContent:=False, Value:=”Test”, _
Type:=msoPropertyTypeString
Debug.Print prps(strProp).Value
AddDocPropsTestExit:
Exit Function
AddDocPropsTestError:
MsgBox “Error No: ” & Err.Number & “; Description: ” & Err.Description
Resume AddDocPropsTestExit
End Function
On the topic of Access custom database properties (which I said I didn’t understand completely), Andy Baron sent me a bit of useful information:
When you add custom properties to an Access database through the UI, they are added to the Properties collection of a document called “UserDefined” in the Databases container. You can iterate through them in code by iterating through
db.Containers(“Databases”).Documents(“UserDefined”).Properties.