Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA - BuiltInProperties

Status
Not open for further replies.

Lissu

Programmer
Nov 19, 2001
3
Hi!

I have a Word template working fine, which I would like to improve a little.

In File - Properties (Word 2003) I use Title, Subject, Author and then I put the fields in header and footer. This I must remember to update manually for each document.

Lazy as I am I would like the template work as follows when I start a new document from template.

VBA should show me a form where I can update the values and then VBA updates the fields (this code I can't find anywhere, doesn't help to record a macro, turns out blank...)

Thanks in advance for any tips!

Lisbeth
aka
Lissu
 
Use the Document.BuiltInDocumentProperties collection.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That should be ActiveDocument.BuiltInDocumentProperties, as in:
Code:
ActiveDocument.BuiltInDocumentProperties("Author") = _
    TextBox1.Text
Assuming TextBox1 is a textbox on the userform.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry, I suggested to use the property of an object, namely the Word.Document one.
 
Huh? There is no Document.BuiltInDocumentProperties collection.

There is:

A) Documents(x).BuiltInDocumentProperties, "x" being the Index number. This is notoriously wacky, as knowing the Index number is not intuitive.

B) Documents("Filename.doc").BuiltInDocumentProperties, which is easier, if you know the name. Note that the ".doc" is required.

C) ActiveDocument.BuiltInDocumentProperties.

As the OP stated:

"I would like the template work as follows when I start a new document from template.

VBA should show me a form where I can update the values and then VBA updates the fields "

Using ActiveDocument.BuiltInDocumentProperties is the best syntax.

A) could work, but you would have to know the index number. I admit if it is the ONLY document open, it would of course be 1.

B) could work, but you would have to either know, or loop through the Documents collection, to find the index number and append it to "Document", to get something like "Document2". Also, depending on circumstances, you could require a very non-intuitive number. If you open a new document (getting "Documentx), and never save it, the number is incremented regardless. Again, making it difficult to get the number.

Open a new doc = Document1
Open a new doc = Document2
Open a new doc = Document3

close Document2 without saving.
close Document3 without saving.

There is only Document1.

Open a new doc = Document4, even though there are only two docs open.

The number appended to "Document" (for new docs) increments for the time the session Word is open. In other words, it is very possible to have "Document17" also be Documents(1).

While using a number with the Documents collection is possible, it is not intuitive (IMO) and tricky to get accurately.

C) will always work, because if the userform is displayed by the cloning of a new document from the template - a .Show in Document_New in the template - then the new document will BE ActiveDocument.



Unless of course other code in Document_New does something else with the Documents.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry, which kind of objects are Documents(x), Documents("Filename.doc") and ActiveDocument for you?
 
Hi Lisbeth,

You could use an autonew macro in the document template, coded as:
Code:
Sub AutoNew()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
With ActiveDocument
  .BuiltInDocumentProperties("Title") = _
      InputBox("Please Insert the Document Title", _
      , .BuiltInDocumentProperties("Title"))
  .BuiltInDocumentProperties("Subject") = _
      InputBox("Please Insert the Subject", _
      , .BuiltInDocumentProperties("Subject"))
  .BuiltInDocumentProperties("Author") = _
      InputBox("Please Insert the Author's Name", _
      , .BuiltInDocumentProperties("Author"))
  For Each oSection In .Sections
    For Each oHeadFoot In oSection.Headers
      If Not oHeadFoot.LinkToPrevious Then _
        oHeadFoot.Range.Fields.Update
    Next
  Next
End With
End Sub
The way I've coded this, if there's already any information in the Author, subject or title fields in the template, they'll be presented to the user when the new document is created. It also means that re-running the macro on an existing document won't leave the user guessing.

Now all you'll need to do is to add the relevant DOCPROPERTY fields to whichever headers you want to add them to - the macro updates them all.

Cheers

[MS MVP - Word]
 
First of all (nobody mentioned that), Word has a built-in functionality to force insertion of Document properties. Go in Word menu to Tools|Options and on Save tab and turn on "Prompt for document properties" and every time you save your document Word will pop-up Document properties dialog asking you to insert document properties before saving.

Otherwise if you insist on VBA solution for this, in your code use ActiveDocument.BuiltinDocumentProperties collection.
 
Macropod: Thanks! Exactly what I wanted!

Thanks to all for trying!

Rgds
Lisbeth
aka
Lissu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top