Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    programmatically adding custom doc properties? (wd97 sr2)

    I have a template that sticks data into documents via fields referring to some built-in and custom document properties. When attaching this template to old documents (which were originally written using an old template with much the same styles, but less functionality), I want to be able to strip out pre-existing custom document properties (if any), then add my ones in a particular order. I can use a "for i =..." loop with "ActiveDocument.CustomDocumentProperties(i).Delete " to lose the old one(s), but how do I add my new ones? The macro recorder doesn't register that I've done ANYTHING, and I can't see anything obvious in the VBA help...

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: programmatically adding custom doc properties? (wd97 sr2)

    <P ID="edit" class=small>Edited by Gary Frieder on 29-Sep-01 01:50.</P>You can add a custom document property with code like this:

    <pre>ActiveDocument.CustomDocumentProperties.Add _
    Name:="TestCustomProp", LinkToContent:=False, Value:="TestPropValue", _
    Type:=msoPropertyTypeString
    </pre>

    (this is adapted from CustomDocumentProperties Example in VBA Help)

    Note that this statement will cause an error if the "TestCustomProp" custom property already exists in the document. If you're always going to run the delete all custom doc properties code before adding any in, that should avoid this error. If you are ever going to need to add a custom doc prop without running the delete all custom doc props code first, you have a couple of options:

    (1) Just add an "On Error Resume Next" statement before the code that adds the custom document property. This has the advantage of being simple, but you won't know whether or not the custom doc pre-existed or not.

    (2) Before the code that adds the custom document property, use a function to test whether or not the custom document property already exists - in which case the code would look like this:

    <pre>Public Sub InsertCustomDocProp()
    If bCustomDocPropExists("TestCustomDocProp") = False Then
    ActiveDocument.CustomDocumentProperties.Add _
    Name:="TestCustomDocProp", LinkToContent:=False, Value:="TestPropValue", _
    Type:=msoPropertyTypeString
    End If
    End Sub
    '=========================
    Public Function bCustomDocPropExists(strCustomDocPropName As String) As Boolean
    Dim i As Long
    Dim objCustDocProps As DocumentProperties
    Set objCustDocProps = ActiveDocument.CustomDocumentProperties
    For i = 1 To objCustDocProps.Count
    If objCustDocProps(i).Name = strCustomDocPropName Then
    bCustomDocPropExists = True
    GoTo FunctionExit
    End If
    Next 'i
    bCustomDocPropExists = False 'not really necessary!
    FunctionExit:
    Set objCustDocProps = Nothing
    End Function</pre>

    Gary

  3. #3
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: programmatically adding custom doc properties? (wd97 sr2)

    Thanks Gary, I'll try this out as soon as I get the opportunity - it certainly looks like it's going to do the trick.
    Much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •