If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
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) |
#2
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
Are you sure it's not working? Have you made changes that would affect the
TOC? Try this one: 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) |
#3
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
Some more info. This is Word 2002 SP-2. I can manually
select the TOC and update it. That's OK. I can do Select All + F9 to update it. That's OK. But if I record a macro that does Select All + F9, it works while I'm recording the macro, but if I play it back then macro stops working. The recorded macro looks like this: Selection.WholeStory Selection.Fields.Update Have I accidentally done something bad in Options? I am forcing the setting for "Update fields when Printing" to be true in the main macro (that's my workaround for updating fields in the header) I'd be very grateful for any insight into this. I need to release this template and macro set for a group it's just too flaky to inflict on innoncent people. |
#4
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
Thanks very much! I'll try it now.
(And yes, the other one is definitely broken. Have tested it myself on other documents and asked two other people to run it on their machines ... same results) -----Original Message----- Are you sure it's not working? Have you made changes that would affect the TOC? Try this one: 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) . |
#5
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
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) . |
#6
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
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) . |
#7
|
|||
|
|||
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) . |
#8
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
I haven't been able to reproduce it on demand either which is why I have
stuck with that code. I don't do much work personally with T'sOC and for the little I do, it is no big deal to use F9 -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org Shauna Kelly wrote: 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) . |
#9
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
I use TOCs and fields extensively in shared group
templates. It is really important to me that the users have a reliable and easy way of updating _all_ fields in a document, not just the TOC. It surprises me that you find the results inconsistent, because in Word 2000 the StoryRanges macro has *always* failed to update fields in the headers for all users of the templates (but perhaps it's because I was bringing templates forward from Word 97?). Now in Word 2002, the StoryRanges macro fails to update the TOC as well. I had two different users test it with the same results, i.e., the StoryRanges macro fails to update the TOC every time. My test document was the world's simplest document: I launched Word using winword.exe /a, created a brand new Normal.dot, did absolutely no formatting changes and used all default headings. I'm just a technical author, but it looks like a bug to me. If it's not, I'd be very interested in knowing what I've done to cause it to fail. I'm grateful to Graham for providing the alternative macros. -- Regards, Kathleen -----Original Message----- I haven't been able to reproduce it on demand either which is why I have stuck with that code. I don't do much work personally with T'sOC and for the little I do, it is no big deal to use F9 -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org Shauna Kelly wrote: 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) . . |
#10
|
|||
|
|||
Can't update TOC programmatically (but it worked yesterday)
When you update a table of contents with F9 or by using the context
menu you are prompted either to update only page numbers or to update the entire table. In VBA, when you reference a table of contents as a TableOfContents object, this corresponds to calling the UpdatePageNumbers and Update methods, respectively. However, if you reference the table of contents as a Field object, you only have access to the Update method. This means that the coders at Microsoft had to choose: should the Update method of the Field class (as well as the Fields collection) update the entire TOC or only the page numbers? Apparently, they chose the latter. Two (simple) examples: Example 1. To update all fields in the main body of the current document, you can use this code: ActiveDocument.Fields.Update 'updates all fields 'in main body of document; 'only page numbers updated 'for TOC fields Example 2. To update all table of contents in the main body of the current document, you could use something like this: For Each toc In ActiveDocument.TablesOfContents toc.Update 'Update entire TOC Next toc -- Stefan Blom "Kathleen" wrote in message ... Some more info. This is Word 2002 SP-2. I can manually select the TOC and update it. That's OK. I can do Select All + F9 to update it. That's OK. But if I record a macro that does Select All + F9, it works while I'm recording the macro, but if I play it back then macro stops working. The recorded macro looks like this: Selection.WholeStory Selection.Fields.Update Have I accidentally done something bad in Options? I am forcing the setting for "Update fields when Printing" to be true in the main macro (that's my workaround for updating fields in the header) I'd be very grateful for any insight into this. I need to release this template and macro set for a group it's just too flaky to inflict on innoncent people. |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Update another table with a Max record query | Ngan | Running & Setting Up Queries | 2 | June 22nd, 2004 05:01 PM |
ie6 sp1 update fails->"wfp: unrecognized version" | Beemer Biker | Outlook Express | 2 | June 14th, 2004 10:36 PM |
Can't update table from Form | sara | Using Forms | 3 | June 11th, 2004 02:12 PM |
Why no update? Help! | Brian | New Users | 1 | May 6th, 2004 11:15 AM |