View Single Post
  #7  
Old July 10th, 2004, 02:05 PM
Shauna Kelly
external usenet poster
 
Posts: n/a
Default Can't update TOC programmatically (but it worked yesterday)

Hi Graham

FWIW, I've found the same problem. That is, in a macro that goes through the
StoryRanges as your UpdateAllFields() does, for some reason TOC fields don't
update. So I tend to tack on the equivalent of your UpdateAllTOC(). I've
seen this in Word 2000 and 2002, but I've never been able to reproduce it on
demand :-(

Shauna

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


"Graham Mayor" wrote in message
...
Fields in different parts of the document structure can be difficult to
address, hence the alternative suggestions.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org




Kathleen wrote:
Graham,
Interesting results. These two work perfectly:

Sub UpdateAllTOC()
Sub Update()


But this one doesn't:

Sub UpdateAllFields()


I'm using Word 2002, SP-2. Thanks again, at least I know
I haven't gone mad.
-- Kathleen


-----Original Message-----
Sub UpdateAllTOC()
Dim oTOC As TableOfContents
For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC
End Sub


or for all fields

Sub Update()
Dim oField As Field
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oTOC As TableOfContents
For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
oField.Update
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
oField.Update
Next oField
End If
Next oFooter
Next oSection
End Sub

For general use, I tend to use:

Sub UpdateAllFields()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

which is listed as an example on my web site at
http://www.gmayor.com/installing_macro.htm and should also work with
TOC.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org




Kathleen wrote:
This is a strange one. I took the standard macro that
updates all fields in the doc from the Microsoft website.
Yesterday it worked. Today it doesn't. Any ideas what I
might have done to the doc to break it? (I can still
manually update the TOC).

Dim aStory As Range
Dim aField As Field

For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory

(By the way, despite what our friends at Microsoft say,
it doesn't update ALL fields, it misses out any that are
in the headers & footers but that's a different story.
bStory, perhaps? I have a workaround for that little
problem that *does* work, if anyone is interested)


.