How to Generate Word Glossary From Index Entries Automatically
🔍 WiseChecker

How to Generate Word Glossary From Index Entries Automatically

You have a Word document with many index entries marked, and now you need a glossary that lists those terms with their definitions or explanations. Manually copying each index entry into a separate table is time-consuming and error-prone. Word does not have a built-in command to convert index entries into a glossary, but you can automate the process using a macro and a few clever steps. This article explains the difference between an index and a glossary, how to extract index entries using a VBA macro, and how to format the output into a clean glossary table.

Key Takeaways: Automating Glossary Generation From Index Entries

  • Alt+F11 to open the VBA editor then Insert > Module: The starting point for running the macro that extracts index entry text.
  • Sub ExtractIndexEntries() macro code: Loops through all XE fields in the document and copies the entry text to a new document.
  • Find and Replace with ^& wildcard: Removes the extra characters like quotation marks and colons that the macro leaves behind.

ADVERTISEMENT

Index vs Glossary: What Each Feature Does

An index in Word is a list of terms with page numbers that point to where each term appears in the document. You mark index entries by selecting text and pressing Alt+Shift+X, which inserts an XE (Index Entry) field. The index is compiled when you insert an INDEX field and update it. It tells the reader where to find a term.

A glossary is a list of terms with their definitions or explanations. It does not include page numbers. Word has no native glossary feature. To create a glossary, you must build it manually or by extracting the term text from index entries and then writing or pasting the definitions next to each term.

The automatic method described here works only if you have already marked index entries with the terms you want in the glossary. If your index entries include subentries or cross-references, those will also appear in the extracted list. You will need to clean them up after extraction.

Steps to Extract Index Entries and Build a Glossary

Follow these steps to run a VBA macro that extracts all index entry text from your document and places it into a new document. After extraction, you format the list into a two-column glossary table.

  1. Open the VBA Editor
    Press Alt+F11 to open the Visual Basic for Applications editor. In the menu bar, click Insert and then Module. A blank code window appears.
  2. Paste the Macro Code
    Copy and paste the following macro into the module window:


    Sub ExtractIndexEntries()
       Dim doc As Document
       Dim newDoc As Document
       Dim fld As Field
       Dim entryText As String
       Dim outputText As String
       Set doc = ActiveDocument
       Set newDoc = Documents.Add
       For Each fld In doc.Fields
          If fld.Type = wdFieldIndexEntry Then
             entryText = fld.Code.Text
             ' Remove the XE prefix and braces
             entryText = Replace(entryText, " XE ", "")
             entryText = Replace(entryText, "\"", "")
             outputText = outputText & entryText & vbCrLf
          End If
       Next fld
       newDoc.Range.Text = outputText
       MsgBox "Index entries extracted to new document."
    End Sub

    The macro loops through every field in the active document. When it finds an XE field, it extracts the text inside the field code, removes the XE command and quotation marks, and writes the result into a new blank document.

  3. Run the Macro
    Press F5 while the cursor is inside the macro code, or click the green Run arrow in the VBA toolbar. A message box confirms the extraction. A new document opens containing one index entry per line.
  4. Clean the Extracted Text
    In the new document, you may see leftover characters such as colons, semicolons, or backslashes from subentry formatting. Press Ctrl+H to open Find and Replace. In the Find what box, type ^& (the wildcard for any character) and leave Replace with empty. Click More >> and check Use wildcards. Click Replace All to remove unwanted characters. Repeat with specific patterns like : or ; if they appear.
  5. Remove Duplicate Entries
    If a term appears in multiple index entries, the list will contain duplicates. Select all text in the new document by pressing Ctrl+A. On the Home tab, click Sort in the Paragraph group. Choose Paragraphs and Text, then click OK. With the sorted list selected, go to the Home tab, click Replace in the Editing group. In the Find what box, type ^&^13 (any character followed by a paragraph mark). In the Replace with box, type ^&. Click More >> and check Use wildcards. Click Replace All repeatedly until the number of replacements is zero. This removes lines that are identical to the line above.
  6. Convert the List to a Glossary Table
    Select all cleaned text. On the Insert tab, click Table and choose Convert Text to Table. In the dialog, set Number of columns to 2. For Separate text at, choose Paragraphs and Other, then type a colon : if your entries contain a colon separating the term from its definition. If your entries are only terms without definitions, set Number of columns to 1. Click OK. Word creates a table with the extracted terms in the left column. You can then add definitions in the right column.

ADVERTISEMENT

Common Issues and Limitations When Extracting Index Entries

The macro returns empty lines or garbled text

This happens when the XE field code contains subentry formatting or cross-references. The macro removes quotation marks but may leave backslashes and extra braces. Use Find and Replace with wildcards to remove patterns like \: or \t. Alternatively, modify the macro to strip all characters after a colon by adding a line that splits the entryText at the colon and keeps only the first part.

Duplicate entries appear after extraction

If the same term is marked as an index entry on multiple pages, the macro extracts it multiple times. The duplicate removal method described in step 5 works for exact duplicates. For near-duplicates with slight variations in spelling or punctuation, manually review the list before converting to a table.

The glossary does not include definitions

Index entries in Word store only the term text and optional subentry text. They do not store definitions. After you extract the terms, you must manually add definitions in the right column of the table. To speed this up, use a second macro that opens a dictionary or a reference document and looks up each term, but that requires custom coding beyond the scope of this article.

Index Entry Extraction vs Manual Glossary Creation: Key Differences

Item Macro Extraction Method Manual Glossary Creation
Time required Under 5 minutes for setup and execution 30 minutes or more depending on document length
Accuracy of term list Matches every marked index entry exactly Prone to typos and missed terms
Handling of duplicates Requires additional cleanup step You control duplication as you type
Need for definitions Terms only; definitions must be added manually You write definitions as you go
Requires VBA knowledge Basic copy-paste and run None

The macro extraction method saves time when you have a large number of index entries. It does not eliminate the need to write definitions, but it gives you a complete and accurate list of terms to work with.

After you have the glossary table, you can sort it alphabetically by selecting the entire table and clicking Sort on the Home tab. Choose Column 1 and Text. This ensures your glossary is in the standard A-to-Z order.

To save the macro for future documents, open the VBA editor, locate the Normal project in the Project Explorer, double-click ThisDocument, and paste the macro there. It will be available in all documents. You can also assign the macro to a keyboard shortcut by going to File > Options > Customize Ribbon > Keyboard shortcuts > Customize, selecting Macros, and choosing Normal.NewMacros.ExtractIndexEntries.

ADVERTISEMENT