BuiltInDocumentProperties in Word
Wow, another month is upon us. Was it just me or did June just fly by?
This month we will be discussing Microsoft Word’s VBA, specifically the
built in document properties. Murali from India sent me the following
request:
Hello Mr. Chad...
I am a computer consultant from India, and I do take up small time
projects... I was really struck where I had to get the no. of pages in an Excel
worksheet thru VBA, and I found your solution given in
http://personal-computer-tutor.com/chad8.htm page, to be
very correct and was very helpful to me. I have another query where I need to get the no. of pages in an MSWord
document thru VBA...hope you can mail me the solution as soon as
possible, as I am struck with it.
Thanks, Regards, Murali
Thank you, Murali. That is a very good question. The page numbers are
actually much easier to access and use in Word, as we will see. However, if
you don’t know how to find them the search can be a bear.
There is a collection in Word’s Object Model called DocumentProperties. That
collection can be broken into two parts: custom properties and built in
properties. As luck would have it the number of pages is saved in the built
in properties. To access it just use the line: ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
Incidentally there are several properties that are built into word that can
be accessed. For example: wdPropertyTitle Title wdPropertyCharacters Number of characters
wdPropertySubject Subject wdPropertySecurity Security
wdPropertyAuthor Author wdPropertyCategory Category
wdPropertyKeywords Keywords wdPropertyFormat Format
wdPropertyComments Comments wdPropertyManager Manager
wdPropertyTemplate Template wdPropertyCompany Company
wdPropertyLastAuthor Last author wdPropertyBytes Number of bytes
wdPropertyRevision Revision number wdPropertyLines Number of lines
wdPropertyAppName Application name wdPropertyParas Number of paragraphs
wdPropertyTimeLastPrinted Last print date wdPropertySlides Number of slides
wdPropertyTimeCreated Creation date wdPropertyNotes Number of notes
wdPropertyTimeLastSaved Last save time wdPropertyHiddenSlides Number of
hidden Slides
wdPropertyVBATotalEdit Total editing time wdPropertyMMClips Number of
multimedia clips
wdPropertyPages Number of pages wdPropertyHyperlinkBase Hyperlink base
wdPropertyWords Number of words wdPropertyCharsWSpaces Number of characters
(with spaces)
Sometimes it is beneficial to create a summary sheet of a document. The
following macro will create one: Sub SummarySheet()
Dim CurrentDoc As Document
Dim SummDoc As Document
Dim Prop As DocumentProperty
Set CurrentDoc = ActiveDocument
Set SummDoc = Documents.Add
On Error Resume Next
For Each Prop In CurrentDoc.BuiltInDocumentProperties
SummDoc.Content.InsertAfter Prop.Name & " = "
SummDoc.Content.InsertAfter Prop.Value
SummDoc.Content.InsertParagraphAfter
Next
Set CurrentDoc = Nothing
Set SummDoc = Nothing
End Sub
Here is the results of that macro (of course the word count, paragraphs and
etc. are counted before the following was pasted so it isn’t included in the
statistics):
Title = BuiltInDocumentProperties in Word
Subject =
Author = Chad K Welch
Keywords =
Comments = Details of the BuiltInDocumentProperties of the Word Object Model
Template = Normal.dot
Last author = Chad K Welch
Revision number = 8
Application name = Microsoft Word 10.0
Last print date =
Creation date = 7/4/2002 8:56:00 AM
Last save time = 7/6/2002 12:01:00 AM
Total editing time = 61
Number of pages = 2
Number of words = 434
Number of characters = 2669
Security = 0
Category = ABC Computers
Format =
Manager =
Company = VBA Magic
Number of bytes = 43008
Number of lines = 115
Number of paragraphs = 90
Number of slides =
Number of notes =
Number of hidden Slides =
Number of multimedia clips =
Hyperlink base =
Number of characters (with spaces) = 3112
Chad K. Welch works as a technician/enabler in
Utah. He is available for
consulting or application programming with Microsoft Office and VBA.
Do you have a question or
tip you’d like to have Chad address in this column? Send an
email to linda@personal-computer-tutor.com
or contact him directly at chad@welchkins.com for more information.
|