Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Updating Table of Contents

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gaston - 29 Aug 2006 23:09 GMT
Hi. I'm trying to update a Table of Contents (TOC) from a macro.  Nothing I
try works.  When I do it manually, I get a prompt that asks me if I want to
replace the TOC.  None of the methods seem to take a parameter for replacing
the TOC, which is what I need to do.  Recording the manual operations
basically gives the same code that I am using, but it doesn't work at run
time.  Here's what I have now:

For Each aTOC In ActiveDocument.TablesOfContents
   aTOC.Update
   aTOC.UpdatePageNumbers
Next aTOC

What is the trick to regenerating a TOC that works every time?

Thanks,

Gaston
Jean-Guy Marcil - 30 Aug 2006 00:20 GMT
Gaston was telling us:
Gaston nous racontait que :

> Hi. I'm trying to update a Table of Contents (TOC) from a macro.
> Nothing I try works.  When I do it manually, I get a prompt that asks
> me if I want to replace the TOC.  None of the methods seem to take a

You get this prompt not when you update a TOC, but when you add one to a
document that already has one.

> parameter for replacing the TOC, which is what I need to do.
> Recording the manual operations basically gives the same code that I
[quoted text clipped - 6 lines]
>
> What is the trick to regenerating a TOC that works every time?

So, in this case, delete the existing TOC and then add a new one by code.

Is this what you actually want?

Why don't you want to simply update it?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Gaston - 30 Aug 2006 01:21 GMT
I _am_ trying to refresh it.  When I do it manually, it sometimes needs to
update the entries, not just the page numbers.  This is when you need to ask
for it to replace the existing TOC (with a totally regenerated one).  I am
not creating the TOC Entries via a macro.  I simply edit the document,
perhaps add a new section using the HeaderN styles, and regenerate the TOC.  
If I do it manually, it works.  If I do it from a macro, it doesn't work.

> Gaston was telling us:
> Gaston nous racontait que :
[quoted text clipped - 22 lines]
>
> Why don't you want to simply update it?
Jean-Guy Marcil - 30 Aug 2006 16:03 GMT
Gaston was telling us:
Gaston nous racontait que :

> I _am_ trying to refresh it.  When I do it manually, it sometimes
> needs to update the entries, not just the page numbers.  This is when
[quoted text clipped - 3 lines]
> styles, and regenerate the TOC. If I do it manually, it works.  If I
> do it from a macro, it doesn't work.

Sorry, but I do not understand.

Let's say I have a TOC,
I place the insertion point anywhere  inside the TOC,
I hit F9 (to update it),
A message pops up asking me if I want to update only the page numbers or the
entire table,
Whatever I select, I never get a second message asking me if I want to
replace the existing TOC.

The only time I get the message asking me if I want to replace the TOC is
when I do Insert > Reference > Index and Tables... > Table of Contents tab >
OK *and* I already have a TOC in the document.

So, if you are updating the TOC manually (by right -clicking on it and
selecting "Update field" or doing F9) and you get the message asking you if
you want to replace the current TOC, then you have to tell us the exact
steps you are taking and commands you are using to create and update the
TOC.

If you want to update the TOC (and not just the page numbers) then this code
should work:

   Dim aToc As TableOfContents

   For Each aToc In ActiveDocument.TablesOfContents
       aToc.Update 'Updates entire TOC
   Next aToc

or (If you have only one TOC in the document)

   ActiveDocument.TablesOfContents(1).Update

In your first post, you wrote
(with new comments of mine betwen the [] ):

"Hi. I'm trying to update a Table of Contents (TOC) from a macro.  Nothing I
try works.  When I do it manually, I get a prompt that asks me if I want to
replace the TOC.  [You should not get this message if you are  simply
updating the TOC manually, if you are, something is wrong with your TOC
or your document] None of the methods seem to take a parameter for replacing
the TOC, which is what I need to do. [Not according to your second post
where
you stated - above - that all you want to do is update the TOC - refresh
means
update, right?]  Recording the manual operations basically gives the same
code
that I am using, but it doesn't work at run time. [What do you mean by
"It doesn't work" It does not update? It does but the content is wrong?
It doesn't update and you get some kind of error message? ...]
Here's what I have now:"

Hoping we can sort it out... :-)

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Gaston - 30 Aug 2006 17:23 GMT
Hi Jean-Guy.

I have confused you with my wording.  Sorry about that.  What I have meant
by the "replace the TOC prompt" is really the "update the entire table"
choice on the prompt you get when doing it manually.

You will notice than in my code I am using the two methods that seem to
correspond to the choices that you get in that prompt dialogue panel.  Yet,
my table of contents is sometimes not updated.  Either the methods are buggy
or there is some other way to do this that I'm not aware of.  Hence my
question.

Merci.

> Gaston was telling us:
> Gaston nous racontait que :
[quoted text clipped - 60 lines]
>
> Hoping we can sort it out... :-)
Jean-Guy Marcil - 30 Aug 2006 20:55 GMT
Gaston was telling us:
Gaston nous racontait que :

> Hi Jean-Guy.
>
[quoted text clipped - 7 lines]
> the methods are buggy or there is some other way to do this that I'm
> not aware of.  Hence my question.

If you use the code I posted in my last reply (Which will update the entire
table, not just the page numbers) and it does not work, then you can try
calling it twice in a row (TOC updating always causes a full document
repagination, sometimes, usually in VBA code, depending on document content,
the TOC is not updated correctly because of this, so it needs to be done
twice. But this is very rare).

Even after this, if the updating fails, it means you are not applying the
styles correctly in the document or the TOC is relying on styles you are not
actually using in the document.

If you are still having trouble, select the TOC, do SHIFT-F9 and post the
field code you see instead of the TOC value (Yes, the TOC is just one single
big field).

Then we can explain what the TOC does and this might explain the
discrepancies you have observed between actual results and expected outcome.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.