Article ID: 182184
Article Last Modified on 11/23/2006
Sub SetCustomPropertyName()
' **************************************************
' This SubRoutine passes the Custom Property Name,
' Value and Property Type to the SetProp subroutine.
' **************************************************
' Set the custom property "MyCustomPropertyName" equal
' to "MyCustomValue". The msoPropertyTypeString constant
' specifies the type of property, and must be included.
SetProp "MyCustomPropertyName", "MyCustomValue", _
msoPropertyTypeString
End Sub
Sub SetProp(CDPName As String, CDPValue As Variant, Optional _
CDPType As Long)
' ***********************************************
' The SetProp routine checks to see if the Custom
' Document Property pre-exists. If it exits, then
' it adds the new value. If it does not exist,
' it creates the new property and adds the new value.
' ***********************************************
' Make sure the optional argument CDPType is set.
' If it is missing, make it a string value.
Dim oCDP, oProp, msg
If IsMissing(CDPType) Then
CDPType = msoPropertyTypeString
End If
Set oCDP = ActiveDocument.CustomDocumentProperties
' Compare each custom document property to the
' property you want to create to see if it exists.
For Each oProp In oCDP
' If the Custom Property exists...
If oProp.Name = CDPName Then
With oProp
' ...the custom property Type you are setting
' must match the pre-existing custom property.
If .Type <> CDPType Then
msg = "The custom property types do not match."
msg = msg + " Custom property not set."
MsgBox msg
' End the routine.
Exit Sub
End If
.LinkToContent = False
' Set the new value.
.Value = CDPValue
End With
' A match was found, so exit the routine.
Exit Sub
End If
Next oProp
' No match was found. Create a new property and value.
oCDP.Add Name:=CDPName, Value:=CDPValue, Type:=CDPType, _
LinkToContent:=False
End Sub
For more information about setting custom properties, click the Office
Assistant while in the Visual Basic Editor, type CustomDocumentProperties
Property, click Search, and then click to view "CustomDocumentProperties
Property."
179216 OFF98: How to Use the Microsoft Office Installer Program
181058 OFF98: How to Run Sample Code from Knowledge Base Articles
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon vb vba vbe
Keywords: kbmacroexample kbprb KB182184