Share via

Macro to update TOC

Anonymous
2021-12-08T08:14:00+00:00

I want a macro that should update the TOC style within the document all TOC headings should be centered and remove page numbers.

Please see the image of how it appears now and how I want it after running the macro.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Suzanne S Barnhill 277.8K Reputation points MVP Volunteer Moderator
    2021-12-08T13:06:47+00:00

    Doug has told you how to remove the page numbers. For more on formatting the TOC, see http://wordfaqs.ssbarnhill.com/TOCTips.htm#Appearance 

    Changing the alignment of any one of the TOC entries should update the paragraph style for that level because TOC styles are by default set to update automatically. For example, if a Level 1 heading is centered, the TOC 1 style will be updated. It should not be necessary to center or recenter the entire TOC.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2021-12-08T10:30:28+00:00

    Press ALT+F9 to toggle on the display of the field codes in the document and inside the closing } of the TOC field, add \n. That will suppress the display of the page numbers. Use ALT+F9 again to toggle off the display of the fields codes and then select Table of Contents and format the text so that it is Centre aligned.

    If you are going to be updating the Table of Contents, you should modify the paragraph formatting in each of the TOC # fields used in it to avoid having the re-center the entries after an update.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  3. Stefan Blom 342.1K Reputation points MVP Volunteer Moderator
    2021-12-09T15:06:23+00:00

    You can set up a template with the desired formatting, including TOC styles, create a new document based on the template and then import existing content via the Insert File dialog box. The template would also have to contain the TOC (formatted to your liking). This will be more efficient than trying to modify styles on a document-by-document basis.

    That said, of course a macro might be useful when dealing with the formatting of TOC entries. You'll need something along these lines:

    Sub TestMacro() 
    
    'Macro created by Stefan Blom, MVP, December 2021 
    
    Dim i As Long 
    
    For i = 1 To 9 
    
    ActiveDocument.Styles("TOC " & CStr(i)) _ 
    
    .ParagraphFormat.Alignment = wdAlignParagraphLeft 
    
    Next i 
    
    End Sub
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Suzanne S Barnhill 277.8K Reputation points MVP Volunteer Moderator
    2021-12-09T13:19:48+00:00

    You could save the formatted TOC (that is, the TOC field) as an AutoText entry; see http://wordfaqs.ssbarnhill.com/TOCTips.htm#SaveWork

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  5. Stefan Blom 342.1K Reputation points MVP Volunteer Moderator
    2021-12-09T10:25:12+00:00

    Once you have modified the settings for the TOC series of styles, you don't have to change them again (in the same document). TOC 1 controls the formatting of level 1 entries in the table of contents, TOC 2 controls the formatting of level 2 entries, etc.

    To get a consistent look, modify these styles in the template you are using.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments