PDA

View Full Version : Solved: More Interesting Quirks of the UpdateField Command



gmaxey
09-15-2005, 07:47 PM
Earlier today I learned that I had inadvertently been using a Word command UpdateFields as a macro name. I had been using this macro for over a year and finally got bit trying to use other code to update a TOC field.

Jay Freedman and I did a little experimenting using Word2003 and I wanted to share the results here:

First insert a blank module in Normal.Dot and add this code


Sub UpdateFields()
MsgBox "Surprise! You've been sent here by your own poor naming."
End Sub


Next in a new blank document project enter the following code:

Sub updateAField()
ActiveDocument.Fields(4).Update
End Sub
Sub UpdateAllFields()
ActiveDocument.Fields.Update
End Sub
Sub updateTOCPageOnly()
ActiveDocument.TablesOfContents(1).UpdatePageNumbers
End Sub
Sub updateEntireTOC()
ActiveDocument.TablesOfContents(1).Update
End Sub

In the document type a line of Heading 1 text, insert a TOC above it. Below the text insert a bit of text book markit and then enter a REF field to the bookmark. Don't try to update yet

Here are some interesting results:

Run the macro UpdateAField. The .Update method works as expected and updates the field.

Change the bookmark text a bit and the heading text a bit and run UpdateAllFields. Here .Update causes the REF field to update but not the "3" fields that make up the TOC. There must be 3 right? Using the code below the REF field has an index value "4" and the field count is 4.


Sub getFieldIndex()
'Select the REF Field
MsgBox Selection.Fields(1).Index
End Sub
Sub CountFields()
MsgBox ActiveDocument.Fields.Count
End Sub


So Fields.Update doesn't update TOC fields.

Now using the enter key drive the heading 1 text to page two and run the macro updateTOCPageOnly(). Works like a champ. Still the TOC text doesn't match the heading 1 text. Now run the macro updateEntireTOC()

Surprised? http://vbaexpress.com/forum/images/smilies/doh.gif I'm not now, but that was what was happening except I didn't have a message box to help me out.

In this instance, unlike the first two, the .Update method is calling the macro which is incorrectly named as a builtin Word command.

Here are some other interesting observations.

Right click the REF field and select Update Field. Surprise again. The context menu Update Field calls the UpdateField

Select the REF field and press F9. Again UpdateField is called

This is really strange. Open the Outline Toolbar, select Update TOC pick the page numbers only option and update. Works like a champ. Now repeat with the update entire table opion. It works!! Now I'm really surprised.

TonyJollans
09-16-2005, 03:24 AM
Hi Greg,

Thanks for giving us the results of your investigation. Some surprising observations!

The TOC seems to have an uneasy relationship with other fields. Field updating, as I'm sure you know, is sometimes more of an art than a science but it's interesting that Fields.Update doesn't update TOCs - I wonder if there are any others it doesn't update.

The three fields which make up the TOC are, of course a variable number of fields. Depending on options chosen, each entry in the TOC (when built) contains a Hyperlink Field and a (rather hidden) PageRef Field. It is, presumably, these fields which the UpdateFields command is trying to update when called from the TOC Update.

The Update TOC option on the Outlining toolbar calls the UpdateTableOfContents Command (try creating a macro of that name - in 2003 and maybe XP (don't have it to hand to check) - before that it was different). It seems that when invoked this way it bypasses the Word UpdateFields command mechanism when updating the hidden fields. I guess it's because it is somehow working directly on the TOC rather than a Range in the document - but it is only a guess. I'm a bit surprised that TablesOfContents(1).Update doesn't call the same routine as the button on the toolbar, but what would life be without these quirks?

Thanks again.

TonyJollans
09-16-2005, 09:08 AM
I've had a little bit more of a play with this and it seems that Fields.Update does update Page Numbers in the TOC - in other words it appears to run Update TOC with the default option of "update page numbers only". It's as though DisplayAlerts was off even though it isn't; quite why, I don't know.

gmaxey
09-16-2005, 03:10 PM
Tony,

Yes you are right. I didn't have a page change to show when I ran that test. I guess that proves the merit of comprehensive testing. :)

MOS MASTER
09-16-2005, 04:27 PM
I've had a little bit more of a play with this and it seems that Fields.Update does update Page Numbers in the TOC - in other words it appears to run Update TOC with the default option of "update page numbers only". It's as though DisplayAlerts was off even though it isn't; quite why, I don't know.

Hi Tony and Greg, :yes

It's because you have to use a different method to update those pagenumbers!

It is UpdatePageNumbers method!

Sample code:
ActiveDocument.TablesOfContents(1).UpdatePageNumbers

Or something like:
Dim oTOC As Word.TableOfContents
For Each oTOC In ActiveDocument.TablesOfContents
oTOC.UpdatePageNumbers
Next oTOC


But for me this still works to update the TOC in Full (I'm probably missing something):
ActiveDocument.TablesOfContents(1).Update


Like you said when updating fields you always have to check carefully if the fields are updated cause there are supporting methods to update special fields! (And I do agree updating fields is a strange thing in Word)

More important when updating fields in other Word Stories (Not mainstory) you have to use the nextStoryRange in Recursive to make sure you cover all!

It's always very nice to look in to this stuff.

HTH, :whistle:

gmaxey
09-16-2005, 05:26 PM
MOS Master, I think the only remaining mystery is why

Sub UpdateEntireTOC()
ActiveDocument.TablesofContents(1).Update
End Sub


Calls the macor Sub UpdateFields and generates the surprise message in the experiments above while using the Update TOC command button on the Outlining Toolbar and selecting Update entire tables doesn't and updates the TOC as expected. :think:

To update all fields (TOCs excluded) I use the following:

Sub myUpdateFields()
Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub

Comments welcomed.

MOS MASTER
09-17-2005, 06:13 PM
MOS Master, I think the only remaining mystery is why

Sub UpdateEntireTOC()
ActiveDocument.TablesofContents(1).Update
End Sub


Calls the macor Sub UpdateFields and generates the surprise message in the experiments above while using the Update TOC command button on the Outlining Toolbar and selecting Update entire tables doesn't and updates the TOC as expected. :think:

Hi Greg, :yes

Indeed but IMO this is something of a bug when using Word commands.

On my PC if I have a sub called UpdateFields and I step through your sub above the code stops at line 2!

So it doesn't go to the UpdateFields sub at all it just aboards (Tested in 2000...will try tomorrow on 2003)

But when I comment out the sub UpdateFields and step through the code again it does go past the second line and update the TOC! :eek:

So this is a bug to me!



To update all fields (TOCs excluded) I use the following:

Sub myUpdateFields()
Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub

Comments welcomed.

The code is excellent and that's the code I meant when I said:

More important when updating fields in other Word Stories (Not mainstory) you have to use the nextStoryRange in Recursive to make sure you cover all!


To include the TOC's I would just append something like this to the code:
Dim oTocs As Word.TableOfContents
For Each oTocs In ActiveDocument.TablesOfContents()
With oTocs
.Update
.UpdatePageNumbers 'to be sure
End With
Next


Later greg...:whistle:

gmaxey
09-17-2005, 06:47 PM
On my PC if I have a sub called UpdateFields and I step through your sub above the code stops at line 2!

So it doesn't go to the UpdateFields sub at all it just aboards (Tested in 2000...will try tomorrow on 2003)

But when I comment out the sub UpdateFields and step through the code again it does go past the second line and update the TOC!



MOS Master. That is how it appeared to me when my Sub UpdateFields() had code to actually update a fields. My macro to update the TOCs just seems to terminate on line 2. When I stripped the UpdateFields of everything and added the msgbox. Then I discovered that the TablesofContents.Update was calling that routine.

BTW. I don't quite understand the "Quote" feature. I may or may not have it right here. If I start a message and then want to quotes a piece from one of your post and then type a bit and quote a piece from someone else, how do I do that.

Thanks.

MOS MASTER
09-17-2005, 06:56 PM
MOS Master. That is how it appeared to me when my Sub UpdateFields() had code to actually update a fields. My macro to update the TOCs just seems to terminate on line 2. When I stripped the UpdateFields of everything and added the msgbox. Then I discovered that the TablesofContents.Update was calling that routine.

I must admit it took me a few readings of this topic before I could reproduce it myself...but like I said it must be a bug in Word.



BTW. I don't quite understand the "Quote" feature. I may or may not have it right here. If I start a message and then want to quotes a piece from one of your post and then type a bit and quote a piece from someone else, how do I do that.

Thanks.

Hi Greg, http://vbaexpress.com/forum/images/smilies/045.gif

Well that's easy.

If You press the quote button on the forum you get something like (I insert quotes to show the tags here) http://vbaexpress.com/forum/images/smilies/112.gif

["QUOTE=gmaxey"] First line
second line
third line[/QUOTE"]

If you want to break that quote just add manual Quote tags. ["Quote] ["/Quote]

Each tag needs an end tag so it's pretty straight forward.

If I wanted to change the above quote I'd do something like:

["QUOTE=gmaxey"] First line
second line ["/Quote]

Broken of on second line
["Quote]
third line[/QUOTE"]

In forum view it looks like:

First line
second line

Broken of on second line


third line

You can also use the Quote button on the formatting toolbar (Most right on the lower row)

Hope I explained it Ok? http://vbaexpress.com/forum/images/smilies/045.gif

gmaxey
09-17-2005, 07:50 PM
I must admit it took me a few readings of this topic before I could reproduce it myself...but like I said it must be a bug in Word.



Hi Greg, http://vbaexpress.com/forum/images/smilies/045.gif

Well that's easy.

If You press the quote button on the forum you get something like (I insert quotes to show the tags here) http://vbaexpress.com/forum/images/smilies/112.gif

["QUOTE=gmaxey"] First line
second line
third line[/QUOTE"]

If you want to break that quote just add manual Quote tags. ["Quote] ["/Quote]

Each tag needs an end tag so it's pretty straight forward.

If I wanted to change the above quote I'd do something like:

["QUOTE=gmaxey"] First line
second line ["/Quote]

Broken of on second line
["Quote]
third line[/QUOTE"]

In forum view it looks like:


Broken of on second line


You can also use the Quote button on the formatting toolbar (Most right on the lower row)

Hope I explained it Ok? http://vbaexpress.com/forum/images/smilies/045.gif

gmaxey
09-17-2005, 07:56 PM
MOS MASTER,

I guess it must be one of those things that is easy after you know how. I am still not getting it or it isn't what I think it is.

Lets say I want to quote just your statement "Well that's easy."

I selected it in your message and then pressed Quote in the bottom right corner of your last message. What I expect is that piece of selected text to be include in my reply. What I get is the text of your entire message. Is this normal? Do I then need to cut out all the rest of your text an leave just "Well that's easy."

Next say I want to make a comment about your quote then insert a quote from another person participating in the thread. How do I do that? Do I just copy the piece of text in my reply :dunno ?

I'll get it. It might just take a while :(

Anne Troy
09-17-2005, 08:04 PM
Hit the Go Advanced button when you reply. It's right below the "quick reply" box. Then, the 2nd toolbar, far right has a "quote" button. I will hit enter twice right now and hit the quote button, and then type something between the tags that appear.


something between the tags that appear

Later, if you frequent a lot (and we hope you will! : pray2:), you'll memorize some of these tags and just type them. I rarely come to the "Go advanced" window when I reply.

Alternatively, you can hit the Quote button next to the reply button. That gives you the entire person's post as a quote in your reply, and you can delete/copy/paste the parts you want/don't want.

Great to have another Word geek in our midst!! :bigkiss:

gmaxey
09-17-2005, 08:14 PM
Dreamboat,

OK here is an experiment.


Hit the Go Advanced button when you reply.

Hopefully that will show a quote from your login.

Now I am going to try to stick in a quote from MOS MASTER


Well that's easy"

Ok, I just previewed and I think I have it. It takes a little more manual effort than I thought.

A suggestion. While composing a message it would be nice if you could scroll down to a previous post, select a bit of text, click a "quote this snipet" button and be returned to your reply with the quote inserted. Wouldn't that be handy?

MOS MASTER
09-20-2005, 03:47 PM
Ok, I just previewed and I think I have it. It takes a little more manual effort than I thought.

Hi Greg, :yes

Glad to see you've got it!

[/quote]
A suggestion. While composing a message it would be nice if you could scroll down to a previous post, select a bit of text, click a "quote this snipet" button and be returned to your reply with the quote inserted. Wouldn't that be handy?[/QUOTE]

Yes that would be handy...but for now we have to scroll down copy the text and paste it in the reply.

The forum has a lot of nice improvements comming but all good things take time.

Later..:whistle:

gmaxey
09-21-2005, 02:52 AM
Thanks for the feedback and help with the quotes.

MOS MASTER
09-21-2005, 03:06 PM
Thanks for the feedback and help with the quotes.

No problem Greg..it's my pleasure! :yes

fumei
09-21-2005, 11:52 PM
I always just type it.....

MOS MASTER
09-22-2005, 02:51 PM
I always just type it.....

Me too buddy. I'm much faster with a lot of quotes typing the tags then using the UI to deal with them.:yes

fumei
09-23-2005, 08:28 AM
Shrug....I guess it is all a matter of what you are comfortable with. I type pretty fast, so I find it faster and more direct to put the tags in myself. Like you.

MOS MASTER
09-23-2005, 04:03 PM
:rofl: I've been acused of having fast fingers in the past too! :rotlaugh: (I still remember the times that I wasn't able to type blind)