I encountered these while browsing a document a long time ago and in what felt like another galaxy. However, I gave it a look, and indeed they are present. So....
I set up a macro recording session to capture the steps necessary.
NOTHING
OK, let's take a look at the object model for Document. Sure enough there is CustomDocumentProperties (nothing about non-custom DocumentProperties). No type given, just Object. Really helpful. However, in the model (using F2) there are entries for CustomProperties, Custom Property, DocumentProperties and DocumentProperty.
OK, not too many possibilities, let's throw some code at it:
Sub TestPrivateProperties()
Dim PrivateProperties As Object
Dim PPCount As Long
Dim PPInstance As Object
Dim PPName As String
Dim PPValue As String
Set PrivateProperties = ActiveDocument.CustomDocumentProperties
PPCount = PrivateProperties.Count
Set PPInstance = PrivateProperties.Item(PPCount)
PPName = PPInstance.Name
PPValue = PPInstance.Value
End Sub
It ran - and found the single custom property I had created manually (while trying to record the macro). OK, Let's redeclare PPInstance as a CustomProperty:
Sub TestPrivateProperties()
Dim PrivateProperties As Object
Dim PPCount As Long
Dim PPInstance As DocumentProperty
Dim TIInstance As DocumentProperty
Dim PPName As String
Dim PPValue As String
Set PrivateProperties = ActiveDocument.CustomDocumentProperties
PPCount = PrivateProperties.Count
Set PPInstance = PrivateProperties.Item(PPCount)
PPName = PPInstance.Name
PPValue = PPInstance.Value
End Sub
OK - an entry in CustomDocumentProperties does indeed seem to be a DocumentProperty. Let's see if we can change the values:
Sub TestPrivateProperties()
Dim PrivateProperties As Object
Dim PPCount As Long
Dim PPInstance As DocumentProperty
Dim TIInstance As DocumentProperty
Dim PPName As String
Dim PPValue As String
Dim PPType As Long
Dim TIName As String
Dim TIValue As String
Dim TIType As Long
Set PrivateProperties = ActiveDocument.CustomDocumentProperties
PPCount = PrivateProperties.Count
Set PPInstance = PrivateProperties.Item(PPCount)
PPName = PPInstance.Name
PPValue = PPInstance.Value
PPType = PPInstance.Type
If PPValue = "New Value" Then
PPInstance.Value = "Modified value"
Else
PPInstance.Value = "New value"
End If
End Sub
Glory be! It works. Only problem is that I have to create the CustomProperty manually. I wonder if I can add an entry.
(Short cut)
Browsing the MSDN site for info on this, I got:
Function Add ( _
Name As String, _
LinkToContent As Boolean, _
Type As Object, _
Value As Object, _
LinkSource As Object _
) As DocumentProperty
'Usage
Dim instance As DocumentProperties
Dim Name As String
Dim LinkToContent As Boolean
Dim Type As Object
Dim Value As Object
Dim LinkSource As Object
Dim returnValue As DocumentProperty
returnValue = instance.Add(Name, LinkToContent, _
Type, Value, LinkSource)
Hmm, I read the Type value as an integer, but it wants an Object? Turns out there are objects to specify the type (a simnple constant would be fartoo easy, I guess.
The description under the example in MSDN says msoPropertyTypeString should work.
Sub TestPrivateProperties()
Dim PrivateProperties As Object
Dim PPCount As Long
Dim PPInstance As DocumentProperty
Dim TIInstance As DocumentProperty
Dim PPName As String
Dim PPValue As String
Dim PPType As Long
Dim TIName As String
Dim TIValue As String
Dim TIType As Long
Set PrivateProperties = ActiveDocument.CustomDocumentProperties
PPCount = PrivateProperties.Count
Set PPInstance = PrivateProperties.Item(PPCount)
PPName = PPInstance.Name
PPValue = PPInstance.Value
PPType = PPInstance.Type
If PPValue = "New Value" Then
PPInstance.Value = "Modified value"
Else
PPInstance.Value = "New value"
End If
TIName = "NewProperty"
TIValue = "New Value"
' Try to createa new DocumentProperty
On Error Resume Next
Set TIInstance = PrivateProperties(PPCount + 1)
If TIInstance Is Nothing Then
On Error GoTo 0
Set TIInstance = ActiveDocument.CustomDocumentProperties.Add(Name:=TIName, _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:=TIValue, _
LinkSource:=Nothing)
PPCount = PrivateProperties.Count
Set TIInstance = PrivateProperties(PPCount)
With TIInstance
.Name = TIName
.Type = PPInstance.Type
.Value = TIValue
End With
Else
With PrivateProperties(PPCount)
.Value = "Changed using only VBA"
End With
End If
End Sub
The Add fails with an automation error (long negative number signifying nothing).
I'm stumped - perhaps it's time for an adult beverage. If anyone has any ideas, or has done this before, some code examples would be great......
Tony
Bookmarks