#1
|
|||
|
|||
Update TOC
Hi
I have TOC in my document. Is there a way I can force the document (Before exiting) to update the TOC or prompt me to update the TOC? Regards Umesh Banga |
#2
|
|||
|
|||
You will have to create a Autoclose macro for that.
1. Press Alt+F11. This will open the Macro code window. 2. On the left hand side, there is a window called Project. Under the Window, go to the Project which has the name of your document. If you double-click on that, it expands and will show "This Document" below. Double-click on "This Window". 2. In the blank window on the right hand side, type the text given below between the dotted lines: ------- Sub AutoClose() ActiveDocument.TablesOfContents(1).Update End Sub ---------- 3. Close the window and save the file. Test and see. Make a change in the headings, and close the document without updating TOC. When you reopen it, you should find the TOC updated. This code assumes that you have only one TOC in your document. If you have more than one, then you have to change the number in the brackets of TablesOfContents() accordingly. If it is the second TOC, then it will be TablesOfContents(2) and so on. |
#3
|
|||
|
|||
Sorry, there is a mistake. Under point no. 2, it should read "Double-click on "This Document" instead of "This Window".
|
#4
|
|||
|
|||
Thanks mate.
How do I make it part of my normal template? |
#5
|
|||
|
|||
Actually I have just created a module. (also after having a closer look at field -- I found that the way our documet is structured - TOC is deceptive. Actually they have used Page References.
Regards Umesh Banga |
#6
|
||||
|
||||
Actually, I'd have thought it would be more useful to ensure the TOC updates upon opening, as that ensures the refernces match any repagination, etc due to printer changes and the like. Adding the following macro to the Normal Template's 'ThisDocument' module will update all fields in every document that gets opened:
Code:
Private Sub Document_Open() Application.ScreenUpdating = False Dim TOC As TableOfContents ' Table of Contents Object Dim TOA As TableOfAuthorities ' Table of Authorities Object Dim TOF As TableOfFigures ' Table of Figures Object Dim Rng As Range ' Word Range Object With ActiveDocument ' Update fields in each Story. ' Note that this may trigger the updating of ASK & FILLIN fields For Each Rng In .StoryRanges .Fields.Update Next ' The following routines are necessary because the foregoing updates only page numbers ' in TOCs, TOAs and TOFs - field updating doesn't update TOC, TOA or TOF contents. ' Loop through Tables Of Contents and update For Each TOC In .TablesOfContents TOC.Update Next ' Loop through Tables Of Authorities and update For Each TOA In .TablesOfAuthorities TOA.Update Next ' Loop through Tables Of Figures and update For Each TOF In .TablesOfFigures TOF.Update Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thanks Paul,
Actually you made me think, and sometimes we print document before even closing it (which means even if we have used open() function - it will not update TOC - since document is not reopened). Is there a way that word can automatically Update TOC before printing. Regards Umesh Banga |
#8
|
||||
|
||||
You could either:
• create a FilePrint sub (which intercepts the File|Print command) to update the fields before opening the Print dialogue; or • use Word's DocumentBeforePrint event to update the fields. In either case, the macro could call the Document_Open sub, or you could put the code from that into a separate macro (which you could run via Alt-F8) and also call by both the Document_Open sub and whichever of the others you want. For an overview of using Document events, see: http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Thanks Paul,
so just creating this module in normal.dotm will work, is that correct. Code:
Private Sub fileprint() Application.ScreenUpdating = False Dim TOC As TableOfContents ' Table of Contents Object Dim TOA As TableOfAuthorities ' Table of Authorities Object Dim TOF As TableOfFigures ' Table of Figures Object Dim Rng As Range ' Word Range Object With ActiveDocument ' Update fields in each Story. ' Note that this may trigger the updating of ASK & FILLIN fields For Each Rng In .StoryRanges .Fields.Update Next ' The following routines are necessary because the foregoing updates only page numbers ' in TOCs, TOAs and TOFs - field updating doesn't update TOC, TOA or TOF contents. ' Loop through Tables Of Contents and update For Each TOC In .TablesOfContents TOC.Update Next ' Loop through Tables Of Authorities and update For Each TOA In .TablesOfAuthorities TOA.Update Next ' Loop through Tables Of Figures and update For Each TOF In .TablesOfFigures TOF.Update Next End With Application.ScreenUpdating = True End Sub Last edited by macropod; 08-09-2012 at 04:42 PM. Reason: Added code tags & formatting |
#10
|
||||
|
||||
No, because you're neither activating the print dialogue (as I said you'd need to) nor, failing that, executing a Printout command. The DocumentBeforePrint obviates the need to deal with those issues and captures print events that a FilePrint sub won't.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Macro TOCTOF not working - with attachment.
Hi Paul,
Thanks. Please find the attached file. I have used the macro --- see macro with name ---- "updateTOCTOFDocumentOpen". It does not work. Regards Umesh Banga |
#12
|
||||
|
||||
There is no code in your attachment (it is a docx file, after all, and docx files cannot contain macros)!
Besides, have you added code like '.Printout' before the final 'End With'?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Here is the normal.dotm
Hope it helps to resolve.
|
#14
|
||||
|
||||
Hi Umesh,
You have lots of modules & forms in that file, but none of them appears to contain the print macro we were discussing. What is the code you are using for that macro? Furthermore, your 'updateTOCTOFDocument_Open' won't work as specified, because you have changed its name and you haven't done the following: Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
Hi Paul,
Actually I have not used Print macro since I could not figure out how to implement it. However I have been using document_open() function provided by you. I have changed the name as you have observed. So do you mean that I should put that in Normal Template 'ThisDocument' module. -- I have done that now. But it does not work properly. see attached documents (Normal.dotm and document with toc). Regards Umesh Banga |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Organizer does not update some styles | Jennifer Murphy | Word | 10 | 08-31-2015 08:54 AM |
live update | gilgold | Excel | 0 | 05-26-2010 10:49 AM |
VBA to update certain (but not all) fields | sparkyrose | Word VBA | 0 | 05-20-2010 12:50 PM |
Project Auto-Update | hB-sys | Project | 0 | 04-15-2010 06:46 AM |
MS update failure | kbstinky | Outlook | 0 | 07-18-2009 10:59 AM |