Share via

can I setup a custom collection in Word VBA to add vocabulary definitions to a Word document?

Edward Evans 20 Reputation points
2026-03-29T16:51:08.7466667+00:00

I want to create a macro in Word VBA so I can add vocabulary definitions to the document, with text on one page, and definitions on facing page for that part of text. If I do this, is the "range" identifyine the position of the word in the document the "key" in the collection? And does the collection self-sort, as seems to me the case with endnotes?


Mod moved from : Microsoft 365 and Office | Word | For home | Windows

Developer technologies | Visual Basic for Applications

Answer accepted by question author
  1. Q&A Assist
    2026-03-29T16:51:26.2733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    A custom Collection in Word VBA can be used to track vocabulary items and their locations, but it will not behave like built‑in endnotes:

    1. A Collection does not self‑sort
    • A VBA Collection is just an ordered set of items in the order they were added.
    • It does not automatically sort like endnotes or footnotes.
    • Items are accessed by index (1, 2, 3, …) or by an optional string key passed to Add, but the order is still the insertion order.
    • To change order, code must copy items into another structure or rebuild the collection in the desired order.
    1. Using a Range as the “key”
    • A Range object identifies a contiguous area in the document by Start and End character positions.
    • Range objects themselves cannot be used as keys in a Collection (keys are strings), but a Range (or a custom class holding a Range) can be stored as the item.
    • If a key is needed, use something like a string based on the range’s Start (for example, CStr(rng.Start)), or a custom ID.
    • Range objects are dynamic: if the document content changes before the range, the Start/End positions move with the text, which is useful for keeping the vocabulary entry tied to the word.
    1. Practical pattern for vocabulary definitions
    • For each vocabulary word:
      • Create a Range pointing to the word in the main text (for example, via ActiveDocument.Words(index) or a search).
      • Store that Range plus the definition text in a custom class or a simple structure (e.g., another Collection or a user‑defined type) and add it to a Collection.
    • When ready to output definitions on the facing page:
      • Iterate the Collection in the desired order (e.g., by document position using Range.Start).
      • If sorting by position is required, extract items to an array or list, sort by Range.Start, then output.
    1. Relation to endnotes
    • The built‑in Footnotes and Endnotes collections have Word’s internal logic for numbering and placement; they are not generic VBA Collection objects.
    • A custom Collection will not automatically renumber or reposition items the way endnotes do; all layout and ordering logic must be implemented in the macro.

    References:

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 460 Reputation points Microsoft External Staff Moderator
    2026-03-31T03:36:00.6266667+00:00

    Hello @Edward Evans ,

    You can store vocabulary entries (term + definition + document position) in a VBA Collection.

    A Word Range itself is not typically used directly as the collection “key”. In VBA, the Key argument of Collection.Add is a unique string expression, so a common approach is to store Range.Start/Range.End with the item and if you want a key, build a string like "Start:End".

    Also, a VBA collection does not self-sort like endnotes appear to. Endnotes/footnotes are a Word feature that Word manages and orders automatically. (See: Insert footnotes and endnotes in Word.)

    Placing definitions on the exact facing page is a separate Word page-layout/pagination problem (page breaks can change as the document is edited), so it helps to understand how Word handles page breaks and pagination.

    I made a small lab to test like this:

    Class Module

    Option Explicit
    
    Public Term As String
    Public Definition As String
    Public StartPos As Long
    Public EndPos As Long
    Public Key As String
    

    Module

    Option Explicit
    
    Public VocabCollection As Collection
    
    Sub InitVocabCollection()
        Set VocabCollection = New Collection
    End Sub
    
    Sub AddSelectedWordToCollection()
        Dim rng As Range
        Dim item As VocabItem
        Dim defText As String
        If VocabCollection Is Nothing Then Set VocabCollection = New Collection
        If Selection.Range.Start = Selection.Range.End Then Exit Sub
        Set rng = Selection.Range
        defText = InputBox("Enter definition for: " & Trim$(rng.Text))
        If defText = "" Then Exit Sub
        Set item = New VocabItem
        item.Term = Trim$(rng.Text)
        item.Definition = defText
        item.StartPos = rng.Start
        item.EndPos = rng.End
        item.Key = CStr(item.StartPos) & ":" & CStr(item.EndPos)
        VocabCollection.Add item, item.Key
    End Sub
    
    Sub ShowCollectionItems()
        Dim i As Long, item As VocabItem, msg As String
        For i = 1 To VocabCollection.Count
            Set item = VocabCollection(i)
            msg = msg & i & ". " & item.Term & " (Start=" & item.StartPos & ")" & vbCrLf
        Next i
        MsgBox msg
    End Sub
    
    Sub ShowItemsSortedByPosition()
        Dim items() As VocabItem
        Dim i As Long, j As Long
        Dim tmp As VocabItem
        Dim msg As String
        ReDim items(1 To VocabCollection.Count)
        For i = 1 To VocabCollection.Count
            Set items(i) = VocabCollection(i)
        Next i
        For i = 1 To UBound(items) - 1
            For j = i + 1 To UBound(items)
                If items(j).StartPos < items(i).StartPos Then
                    Set tmp = items(i)
                    Set items(i) = items(j)
                    Set items(j) = tmp
                End If
            Next j
        Next i
        For i = 1 To UBound(items)
            msg = msg & i & ". " & items(i).Term & " (Start=" & items(i).StartPos & ")" & vbCrLf
        Next i
        MsgBox msg
    End Sub
    

    In my test,

    User's image

    I added the terms in this order: Cherry, then Apple, then Banana. When I ran ShowCollectionItems(), the collection displayed the items as Cherry -> Apple -> Banana, which confirms it keeps insertion order.

    User's image

    After I manually sorted the items by StartPos, ShowItemsSortedByPosition() displayed Apple -> Banana -> Cherry, which matches the document order.

    User's image

    Hope this clarifies your question. If you found my answer helpful, please follow this guide to give feedback.

    Thank you.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.