Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-30-2012, 04:18 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-31-2012, 05:44 AM
Venky62 Venky62 is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Post

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.
Reply With Quote
  #3  
Old 07-31-2012, 05:46 AM
Venky62 Venky62 is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

Sorry, there is a mistake. Under point no. 2, it should read "Double-click on "This Document" instead of "This Window".
Reply With Quote
  #4  
Old 08-01-2012, 06:43 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

Thanks mate.

How do I make it part of my normal template?
Reply With Quote
  #5  
Old 08-01-2012, 06:52 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 08-01-2012, 09:28 PM
macropod's Avatar
macropod macropod is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Note: Venky's code will throw an error if the document doesn't have a Table Of Contents.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 08-01-2012, 09:42 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 08-01-2012, 09:47 PM
macropod's Avatar
macropod macropod is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 08-01-2012, 09:58 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 08-01-2012, 10:02 PM
macropod's Avatar
macropod macropod is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 08-09-2012, 04:38 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default 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
Attached Files
File Type: docx Paul Macro - Update TOCTOATOF.docx (37.1 KB, 16 views)
Reply With Quote
  #12  
Old 08-09-2012, 04:45 PM
macropod's Avatar
macropod macropod is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 08-12-2012, 04:41 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default Here is the normal.dotm

Hope it helps to resolve.
Attached Files
File Type: zip Normal.zip (721.7 KB, 16 views)
Reply With Quote
  #14  
Old 08-12-2012, 05:21 PM
macropod's Avatar
macropod macropod is offline Update TOC Windows 7 64bit Update TOC Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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:
Adding the following macro to the Normal Template's 'ThisDocument' module
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 08-12-2012, 06:49 PM
ubns ubns is offline Update TOC Windows 7 32bit Update TOC Office 2010 32bit
Competent Performer
Update TOC
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

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
Attached Files
File Type: zip Macro.zip (793.2 KB, 16 views)
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update TOC 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft