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 :
> 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