Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

vb macro question

Options
  • 10-01-2011 10:54am
    #1
    Registered Users 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 Posts: 39,853 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 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 Posts: 39,853 Mod ✭✭✭✭Seth Brundle


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


  • Registered Users 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 Posts: 39,853 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 Posts: 3,808 ✭✭✭FatherLen


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


Advertisement