MS Word VBA - update specific docproperty

E

ernmeg

I have a document that has docproperties in the header and body. I have
a custom button that loads a userform in which these document
properties can be changed. Right now, I have the following code that
updates all the fields on the page:

Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
myStoryRange.Fields.Update
Next myStoryRange


This works, however, I'd like to be able to ONLY update the 3 document
properties that have changed. Is there any way to do this?
 
J

Jonathan West

I have a document that has docproperties in the header and body. I have
a custom button that loads a userform in which these document
properties can be changed. Right now, I have the following code that
updates all the fields on the page:

Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
myStoryRange.Fields.Update
Next myStoryRange


This works, however, I'd like to be able to ONLY update the 3 document
properties that have changed. Is there any way to do this?

Dim myStoryRange As Range
Dim oField as Field
For Each myStoryRange In ActiveDocument.StoryRanges
For each oField in myStoryRange.Fields
If oField.Type = wdFieldDocProperty Then
oField.Update
End If
Next oField
Next myStoryRange


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
E

ernmeg

Thanks a lot Jonathan! Is there any way to avoid looping through all
the fields in the range and only update specific fields?
 
J

Jonathan West

Thanks a lot Jonathan! Is there any way to avoid looping through all
the fields in the range and only update specific fields?

If you know exactlly which fields you want, you could do it something like
this

With ActiveDocument.Range
.Fields(1).Update
.Fields(3).Update
.Fields(47).Update
End With

but that will only work if your document doesn't change to the extent of
adding or removing fields. You might be safer updating all fields of a
specific type.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jonathan West

Ah I see, no way to access the fields by name?

I suppose you you mark the field with a bookmark, and then reference the
first field in the range marked by the bookmark. Something like this.

ActiveDocument.Bookmarks("MyBookmark").Range.Fields(1).Update


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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