Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1514
  • Last Modified:

VBA Word 2003 - How do you add and retrieve custom doc properties

Hello,

I have a word template that has a dialog box attached in which the user enters information to generate the document.  I would like to take the text field info and attach it to the document properties.  Later, I want the user to be able to run a different macro -- which will pull that document property information into the new macros dialog box.  (Trying to avoid the user having to enter the same information twice.)

I'm open to any other suggestions for storing the data if doc properties is not the way to go.. Either way, can you tell me how to do this?

Thank you so much!  ~JTG
0
jtaylerg
Asked:
jtaylerg
  • 2
1 Solution
 
Chris BottomleyCommented:
Hi,

If you want the data to be available after closing the file then from my experience Document Properties will do the job.
To read the custom property:

If CustPropExists("Any_Name_string_a") = True Then
' i.e. it exists so upload it
    your_form_name.your_textbox_name = ActiveDocument.CustomDocumentProperties("Any_Name_string_a").Value
else
' Either initialise the value if wanted
    With ActiveDocument.CustomDocumentProperties
        .Add Name:="Any_Name_string_a", _
        LinkToContent:=False, _
        Type:=msoPropertyTypeString, _
        Value:="a string of your choosing as the value of Any_Name_string_a"
    End With
' and/or do anything you want to do if it doesn't exist
End If

' To set your data into the custom property then the above can be used of course as in the first option of the else clause.
0
 
Chris BottomleyCommented:
The custpropexists function is defined as:

Function CustPropExists(str_Property_Name As String) As Boolean
    Dim doc_Property As DocumentProperty
    CustPropExists = False
    For Each doc_Property In ActiveDocument.CustomDocumentProperties
        If LCase(doc_Property.Name) = LCase(str_Property_Name) Then
            CustPropExists = True
            Exit For
        End If
    Next doc_Property
End Function
0
 
jtaylergAuthor Commented:
Thanks Chris,

I need to play with it some more -- but I think it's working thus far.

Have a great week -- JTG
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now