Change Title property in Word 2007

J

Jose Valdes

In Word 2007, I am trying to change the Title property. Here's my fledging
attempt:

ActiveDocument.BuiltInDocumentProperties(Title) = "Junk"

Of course, this fails every time. I suspect that I am doing a lame job of
referencing the member of the collection because the following code works:

ActiveDocument.BuiltInDocumentProperties(1) = "Junk"

Is it possible to access the title by the name of the member?

Thanks!
José
 
J

Jan Hyde (VB MVP)

"Jose Valdes"
<jose.valdes(no_spam_please)@weatherford.com>'s wild
thoughts were released on Fri, 7 Nov 2008 12:57:31 -0600
bearing the following fruit:
 
J

Jose Valdes

I posted my original question because the MS Word Object Model Reference
does a poor job of explaining BuiltInDocumentProperties. Can anyone suggest
a more comprehensive reference for this object model?

Thanks!
José
 
J

Jay Freedman

Hi José,

The problem you had isn't specific to the BuiltInDocumentProperties collection;
it's related to the way VBA addresses specific members of any collection.

All collections have an Item method that returns a member of the collection. The
method takes one argument, which the Help topic calls "index". The index can be
an integer from 1 to the number of objects in the collection; or (for most
collections) it can be a string that was assigned to the member when it was
added to the collection.

As a concrete example, the member of the BuiltInDocumentProperties collection
that contains the document title is the first member, so its integer index is 1.
Its string index is "Title". (The quote marks are required to tell VBA that this
is a literal string and not a variable name.) So each of these expressions would
return the same value:

result = ActiveDocument.BuiltInDocumentProperties.Item(1)
result = ActiveDocument.BuiltInDocumentProperties.Item("Title")

There are other ways to pass an integer or a string to the Item method. For an
integer, you can use a named constant (technically, an "enumeration" value). The
name wdPropertyTitle represents an integer value of 1. Or you can assign a
variable that holds integer values, perhaps to use it in a loop. So these are
equivalent to the first expression above:

result = ActiveDocument.BuiltInDocumentProperties.Item(wdPropertyTitle)

Dim nIndex As Integer
nIndex = 1
result = ActiveDocument.BuiltInDocumentProperties.Item(nIndex)

Similarly, you can use a variable that holds string values, like this:

Dim sIndex As String
sIndex = "Title"
result = ActiveDocument.BuiltInDocumentProperties.Item(sIndex)

In this example, note that the variable name does not have quote marks, but the
literal value does have quotes.

Finally, instead of explicitly calling the Item method as above, you can apply
the index directly to the collection's name as if it was an array. The
expressions above can be written as

result = ActiveDocument.BuiltInDocumentProperties(1)
result = ActiveDocument.BuiltInDocumentProperties("Title")

and so on.

For a reference on this particular point, you can look at "Collection Indexes"
in Chip Pearson's page at http://www.cpearson.com/excel/optimize.htm or
"Accessing an Item in a Collection" in
http://www.functionx.com/vbaexcel/Lesson08.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

Jose Valdes

Hey Jay,

Thank you for the free lesson in accessing collections. It's helpful info
for a newbie like me.

I guess I just got carried away when I slammed Microsoft's documentation for
Word's object model. My frustration with the Help system was that I could
not find a list of members of the BuiltInDocumentProperties collection.
Since I couldn't find it, I assumed that such a list did not exist. However,
I just did a search on wdPropertyTitle and found a Help topic called,
"WdBuiltInProperty Enumeration." The info I wanted was there after all.

Thanks!
José
 
J

Jay Freedman

You're welcome! :)

Jose said:
Hey Jay,

Thank you for the free lesson in accessing collections. It's helpful
info for a newbie like me.

I guess I just got carried away when I slammed Microsoft's
documentation for Word's object model. My frustration with the Help
system was that I could not find a list of members of the
BuiltInDocumentProperties collection. Since I couldn't find it, I
assumed that such a list did not exist. However, I just did a search
on wdPropertyTitle and found a Help topic called, "WdBuiltInProperty
Enumeration." The info I wanted was there after all.
Thanks!
José
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top