Create a Word Glossary – Part 2
A couple of weeks ago, I looked at how to create a Glossary in Microsoft Word. While a little late (and my apologies to John and others for the delay), here is the second part to that post where we look at how to automatically remove pages numbers from the Word-generated Table of Authorities (TOA).
While we can get 90% of the way there, we still need some VBA automation in order to remove the automatically-inserted page numbers (not really of any use in a glossary).
Step 1 – Making friends : application & document awareness
Our first task is to create a Class Module in our Word macro that can “listen” to events within the main document body. Let’s start by pressing <ALT><F11> in Word to bring up our VBA Editor. Broadly speaking, the tasks we want to kick off with are:
- We want to respond to events.
- We need an application variable (declared with WithEvents) to receive the events.
- We need a class module to serve as a container for the application variable.
Let’s set this up by adding a class module called ThisApplication. Right-click on the document project in the Project Explorer and select Insert -> Class Module (note that it’s the class module we’re after). Rename the new module to “ThisApplication”. Then, below the Option Explicit declaration we’ll include the following line of code:
Public WithEvents App As Word.Application
That’s it. Done. Next we need a module whose responsibility is to recognise the document currently in use and listen for any time the document’s fields are updated.
Step 2 – Listen closely : wiring up the application’s UpdateChanges event
First things first. Let’s create that new module we were just talking about. Right-click the Project and select Insert -> Module (just your regular, garden-variety module this time) and rename it to something meaningful. I would suggest Glossary or something along those lines. This is where all the action happens. Your Project should now resemble something similar (i.e. very similar) to the list of files you can see in the image on the right (highlighted yellow, for your convenience).
The first task for this module is going to be to get a reference to the Application whose events we have previously decided we’ll be listening to. Enter the following code after Option Explicit:
Dim MyApplication As New ThisApplication
That defines a container for our application. Let’s actually tell it what to put in there:
Public Sub AutoExec() Set MyApplication.App = Word.Application End Sub
Easy. Now we need to tell Word what to do when the user updates the TOA. Oddly enough, there is no explicitly-defined event for fields updating (e.g. OnFieldUpdating()) in the Word object model. There is something we can use however: Word has an UpdateFields method that we can piggyback off to include our own code. Let’s see how that would look:
' Override Word's native UpdateFields property Public Sub UpdateFields() Dim docThis As Word.Document Dim TOA As TableOfAuthorities Dim parEntry As Paragraph ' Make sure we're working with the currently active document Set docThis = ThisDocument ' Step through the Table of Authorities in the document For Each TOA In docThis.TablesOfAuthorities TOA.Update ' Update the Table of Authorities to include any newly-marked items ' Step through each entry in the Table of Authorities For Each parEntry In TOA.Range.Paragraphs StripPageNumbers parEntry ' Call our StripPageNumbers routine Next Next ' Don't forget to clean up! Set docThis = Nothing End Sub
That’s about it. Let’s look at what we’ve just done here.
We’re working with three objects in the UpdateFields method: docThis (our Document), TOA (our Table of Authorities) and parEntry (the entries in the TOA). The first task is to get a handle on the current document (Set docThis = ThisDocument). Then, for each Table of Authorities in our document, we want to step through each paragraph and strip the numbers from the end of that entry.
Please note at this point that if you’re already using Tables of Authorities in other places, you’ll want to replace the For Each loop with:
Set TOA = docThis.TableOfAuthorities(x)
Where x is the 1-based index number of the TOA (i.e. the order in which your desired TOA appears in the document relative to other TOAs).
Step 3 – Strip! : Remove numeric values from End of Line
Ok. The last thing we need to do is actually get rid of those page numbers! You will have noticed the StripPageNumbers call from the code snippet above. This is the actual code for that method:
' Removes any number from the end of a paragraph Private Sub StripPageNumbers(ByRef Para As Paragraph) ' Make sure we have at least one word in our paragraph (a new line counts as one word, remember). If Para.Range.Words.Count > 1 Then ' Loop until you can't find a number at the end. Do While IsNumeric(Para.Range.Words(Para.Range.Words.Count - 1)) Para.Range.Words(Para.Range.Words.Count - 1).Delete Loop ' Also check to ensure that the last word doesn't have a page number against it ' if the user has forgotten to include a fullstop at the end of the description Do While IsNumeric(Para.Range.Characters(Len(Para.Range) - 1)) Para.Range.Characters(Len(Para.Range) - 1).Delete Loop End If End Sub
Again, let’s look at what we’ve accomplished here. The StripPageNumbers method takes one parameter, Para, which is of type Paragraph. As long as that paragraph is not empty, we check the last word to see if it is a number and if so, we remove it. We then repeat this process for cases where there is no full-stop at the end of a definition.
And we’re done!
A download of the code in this example can be found below. Let me know if this post was useful to you or if you have found any bugs or glitches with my approach. Also – let me know if there are other aspects or problems with Microsoft Office products that you could use some help on.




Thanks for this post. I was wondering how this differs from using an index table to create a glossary. Are they about the same or is one better than the other. Also, is the macro usable for an index as well to remove the page numbers?
In essence, this approach doesn’t differ from using an index – the two tables are essentially the same type of structure with different meta-data flagged throughout the document. However my personal view is that Tables of Authorities are better suited to glossaries partly because an index is much more commonly used and therefore should be preserved and partly because TOAs allow you to categorise your entries, meaning you can still use them for the purpose for which they were intended simply be selecting a different category. Also, the TOA allows you to include a definition for a term in addition to simply marking it. An index only allows you to mark a word or phrase and has a different set of options for elaborating on that marked entry. So in short, the TOA is best suited to the task.
The macro is definitely usable for removing page numbers from an index, but you’d need to modify the variable type in the UpdateFields method to use an Index object instead of the TableOfAuthorities (i.e.
Dim Idx As IndexreplacesDim TOA as TableOfAuthorities).Thanks for the feedback!
Thanks a bunch very helpful
Very nice post! Can you tell me how I can get rid of the TOA Field that word posts in the document when I mark a citation. I rather it never show up at all and not be visible. Thanks!
You won’t want to actually get rid of the field itself – those are what Word uses to recognise the terms you’ve marked for your glossary.
When you mark your terms, the field is marked by Word and some metadata is inserted into your document:
{TA \l "Chrome: Google's own web browser" \s "Chrome" \c 1}However, to provide some visual feedback on the exact term that you’ve marked, it toggles “hidden formatting” on. You’ll recognise the Show / Hide Hidden Formatting button: ¶
To hide it again, just click that button off again.
Thank you SOOOOO much!! This was very helpful!!!
That’s my two pence…
Arial ;o)
http://www.ArialBurnz.com
http://www.EnchantedTutoring.com