An implementation of Visual Basic that is built into Microsoft products.
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:
- A
Collectiondoes not self‑sort
- A VBA
Collectionis 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.
- Using a Range as the “key”
- A
Rangeobject identifies a contiguous area in the document byStartandEndcharacter positions. -
Rangeobjects themselves cannot be used as keys in aCollection(keys are strings), but aRange(or a custom class holding aRange) 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. -
Rangeobjects are dynamic: if the document content changes before the range, theStart/Endpositions move with the text, which is useful for keeping the vocabulary entry tied to the word.
- Practical pattern for vocabulary definitions
- For each vocabulary word:
- Create a
Rangepointing to the word in the main text (for example, viaActiveDocument.Words(index)or a search). - Store that
Rangeplus the definition text in a custom class or a simple structure (e.g., anotherCollectionor a user‑defined type) and add it to aCollection.
- Create a
- When ready to output definitions on the facing page:
- Iterate the
Collectionin the desired order (e.g., by document position usingRange.Start). - If sorting by position is required, extract items to an array or list, sort by
Range.Start, then output.
- Iterate the
- Relation to endnotes
- The built‑in
FootnotesandEndnotescollections have Word’s internal logic for numbering and placement; they are not generic VBACollectionobjects. - A custom
Collectionwill not automatically renumber or reposition items the way endnotes do; all layout and ordering logic must be implemented in the macro.
References: