Monday, April 19, 2010

Word: Show Me My Property

Word documents have many user-accessible properties, such as Author, Title, etc.  One can find them by clicking on the Word Button -> Prepare -> Properties.  These properties can also be accessed by a macro using the BuiltInDocumentProperties method.

This post will show you which properties are available and how to access them using macros.


To access individual properties, use this:

Sub showProperty()
    Set myProp = ActiveDocument.BuiltInDocumentProperties(wdPropertyAuthor)
    'MsgBox ("Value of this property ->" + myProp + "<-")
End Sub

(I like to enclose my displayed values with some character, like an angle bracket, to make it easy to see any preceding or trailing spaces)

In place of wdPropertyAuthor, you can use any of the following properties:
  • wdPropertyAppName
  • wdPropertyAuthor
  • wdPropertyBytes
  • wdPropertyCategory
  • wdPropertyCharacters
  • wdPropertyCharsWSpaces
  • wdPropertyComments
  • wdPropertyCompany
  • wdPropertyFormat
  • wdPropertyHiddenSlides
  • wdPropertyHyperlinkBase
  • wdPropertyKeywords
  • wdPropertyLastAuthor
  • wdPropertyLines
  • wdPropertyManager
  • wdPropertyMMClips
  • wdPropertyNotes
  • wdPropertyPages
  • wdPropertyParas
  • wdPropertyRevision
  • wdPropertySecurity
  • wdPropertySlides
  • wdPropertySubject
  • wdPropertyTemplate
  • wdPropertyTimeCreated
  • wdPropertyTimeLastPrinted
  • wdPropertyTimeLastSaved
  • wdPropertyTitle
  • wdPropertyVBATotalEdit
  • wdPropertyWords
To list all properties in the document, run this macro.  It will put them at the end of the document.

Sub ListProperties()
    Dim rngDoc As Range
    Dim proDoc As DocumentProperty

    Set rngDoc = ActiveDocument.Content

    rngDoc.Collapse Direction:=wdCollapseEnd

    For Each proDoc In ActiveDocument.BuiltInDocumentProperties
        With rngDoc
            .InsertParagraphAfter
            .InsertAfter proDoc.Name & "= "
            On Error Resume Next
            .InsertAfter proDoc.Value
        End With
    Next
End Sub

[source Microsoft]

No comments:

Post a Comment