Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

vb macro question

  • 10-01-2011 10:54AM
    #1
    Registered Users, Registered Users 2 Posts: 3,808 ✭✭✭


    hi i have been asked to write a vb macro in word that creates a table with the existing titles in the document put into the table. i have made the macro ok but i need it to run before a save event but dont have a clue how to do it. any ideas?
    any help is greatly appreciated.


Comments

  • Moderators, Politics Moderators, Paid Member Posts: 44,042 Mod ✭✭✭✭Seth Brundle


    What do yu mean by existing titles? Are these headings? If so, why not use a table of contents using that heading size alone?


  • Registered Users, Registered Users 2 Posts: 3,808 ✭✭✭FatherLen


    yeah sorry i am doing that. i am taking headings of a certain size(the second headings in this case) and putting them into a table but i need the macro to fire when the file is saved(preferably before). i have found a few bits online about this in excel macros but very little for word. i am using word 2010 btw.
    thanks again.


  • Moderators, Politics Moderators, Paid Member Posts: 44,042 Mod ✭✭✭✭Seth Brundle


    So am I correct that you want the table of contents to refresh?


  • Registered Users, Registered Users 2 Posts: 3,808 ✭✭✭FatherLen


    yeah. sorry about my lack of kowledge with this subject, its my first time dealing with vb.
    thanks for your help.


  • Moderators, Politics Moderators, Paid Member Posts: 44,042 Mod ✭✭✭✭Seth Brundle


    There is no onSave event in Word.
    I've gone with the macro running when the document is closing - change as required.
    With your word document open (and for the moment close all others), press Alt & F11.
    In the ThisDocument module (visible in the project Explorer Ctrl & R) paste in the following...
    Sub AutoClose()
    Call UpdateRefTables
    End Sub
    

    Then go to Insert > Module and paste the following
    Sub UpdateRefTables()
    Dim TOC As TableOfContents  ' Table of Contents Object
    Dim TOA As TableOfAuthorities ' Table of Authorities Object
    Dim TOF As TableOfFigures ' Table of Figures Object
    With ActiveDocument
    ' The following routines 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
    End Sub
    

    Edit: Click Debug > Compile Project

    Save this as a macro enabled document (.docm) and make sure that users know to enable macros when opening.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,808 ✭✭✭FatherLen


    cheers buddy this worked perfectly. it updates the toc everytime i close the file. thanks again so much.


Advertisement