Populate file properties from text fields in document

  • Thread starter Thread starter Embobz
  • Start date Start date
E

Embobz

I am trying to get the Subject and Title from the file properties of a
document to be set automatically from specified text fields in the
body of the document. I can quite easily go the other way by typing
text into the title and subject fields in the properties dialog box,
and inserting relevant fields into the document, but that's not what I
want. Is there a way to do it using macros, or can I insert a field
so that when a user edits the field, it will automatically update the
file properties. Is this even possible? I consider myself an
'advanced user' of Word, but with some limited experience in coding,
just so you'll know where to pitch any answer to me. Thanks.
 
Set the following macro to run on exit from text formfields Text1 and
Text2


Sub OnExitTF()
Dim oFF As FormFields
Set oFF = ActiveDocument.FormFields
Dim pStr As String
If Selection.FormFields.Count = 1 Then
pStr = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count >
0 Then
pStr = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
Select Case pStr
Case "Text1"
ActiveDocument.BuiltInDocumentProperties("Title").Value =
oFF("Text1").Result
Case "Text2"
ActiveDocument.BuiltInDocumentProperties("Subject").Value =
oFF("Text2").Result
End Select
End Sub
 
If you have Word 2007 then all you have to do is insert bound Document
Property fields in your document. They are found on the Insert tab under
Quick Parts.

Very cool, I might add. You can update the document properties or use the
bound doc property fields to update the properties. You can also add
multiple bound doc property fields if you need the data repeated in your
document. All of the fields and the properties update automatically - all
you have to do is insert the field. :-)

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Co-author of Word 2007 Inside Out:
http://www.microsoft.com/MSPress/books/9801.aspx#AboutTheBook

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
Greg,

Thanks for the code. It does what I want it to do, but I'm having
problems getting the macro to run 'on exit'. By the way, it's Word
2003 - I didn't mention that. Either I'm being a bit dim, or I didn't
explain properly exactly what I was trying to do. I need to have some
existing text that will tell the user that they need to 'type subject
here' etc. but then whatever they type is then put into the subject/
title field of the properties. The problem I'm having is that as soon
as you type into the text formfield, it is replaced by the normal
text, the text formfield no longer exists, and so the macro cannot run
'on exit' from a non-existent field. More help please!

Emma
 
If you have Word 2007 then all you have to do is insert bound Document
Property fields in your document. They are found on the Insert tab under
Quick Parts.

Very cool, I might add. You can update the document properties or use the
bound doc property fields to update the properties. You can also add
multiple bound doc property fields if you need the data repeated in your
document. All of the fields and the properties update automatically - all
you have to do is insert the field. :-)
Beth,
That does sound very cool. I wish that it were Word 2007 - it sounds
like life would be much simpler! Unfortunately it is Word 2003.
Emma
 
Back
Top