Share via

in Microsoft Word VBA, can you use a range for the location of a word to extract the item number of that word in the words collectiom=n?

Edward Evans 20 Reputation points
2026-03-26T17:29:32.3533333+00:00

in Microsoft Word VBA, can you use a range for the location of a word to extract the item number of that word in the words collection? I want to arrange vocabulary words for a document so that they are in proper order so that vocabulary words for given section of document are facing page of that portion of document。I want to be able to keep vocabulary words in proper order same way endnotes are kept in proper order


Mod moved from : Microsoft 365 and Office | Word | Other | Windows

Developer technologies | Visual Basic for Applications
0 comments No comments

2 answers

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

    Hello @Edward Evans ,

    You can use a Range to locate a word and calculate its position in the document by counting the words before that range. Word VBA does not provide a direct built-in property for the word’s index in the Words collection, so it usually has to be derived.

    This is a minimal sample from my side:

    Sub NoteCurrentVocabularyWord()
        Dim doc As Document
        Dim vocabRange As Range
        Dim tempRange As Range
        Dim wordIndex As Long
        Set doc = ActiveDocument
        Set vocabRange = Selection.Range
        vocabRange.Expand wdWord
        Set tempRange = doc.Range(Start:=0, End:=vocabRange.Start)
        wordIndex = tempRange.Words.Count + 1
        MsgBox "Vocabulary word: " & Trim(vocabRange.Text) & vbCrLf & _
               "Start position: " & vocabRange.Start & vbCrLf & _
               "Word index: " & wordIndex
    End Sub
    

    To test it, click inside a word in Word and run the macro. It will show the word, its start position, and its calculated index. If your main goal is to keep vocabulary in document order, using Range.Start is often the more reliable method.

    User's image

    Hope this clarifies your question. If you found my answer helpful, thank you much for following this guide to give feedback.

    Thank you.


  2. Q&A Assist
    2026-03-26T17:29:45.1166667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Yes. A Range that represents a specific word can be used to get that word’s index in the Words collection.

    In Word VBA, each Range has a Words collection, and each item in that collection is itself a Range. The Words.Item(Index) method returns the Range for the word at that ordinal position.

    To get the item number (index) of a given word Range within its parent Range’s Words collection, compare it to each Words(i) until a match is found using IsEqual on the Range objects.

    Example VBA pattern:

    Function WordIndexInRange(ByVal rngWord As Word.Range) As Long
        Dim rngParent As Word.Range
        Dim i As Long
    
        ' Parent range whose Words collection is to be searched
        Set rngParent = rngWord.StoryRange  ' or another Range that contains rngWord
    
        For i = 1 To rngParent.Words.Count
            If rngParent.Words(i).IsEqual(rngWord) Then
                WordIndexInRange = i
                Exit Function
            End If
        Next i
    
        WordIndexInRange = 0  ' not found
    End Function
    

    Key points from the object model:

    • Range.Words returns a Words collection representing all words in that Range, including punctuation and paragraph marks.
    • Words.Item(Index) returns a Range for the word at that position.
    • Once the index is known, vocabulary words can be ordered or processed according to their position in the document, similar in concept to how collections like endnotes are kept in order.

    These same principles apply if using the Word Primary Interop Assemblies (PIA) from .NET: Range.Words returns a Words collection, and Words.Item[int Index] returns the Range at that position.


    References:

    0 comments No comments

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.