Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List all occurrences or instances of a word in Word 2

Status
Not open for further replies.

sxschech

Technical User
Jul 11, 2002
1,034
US
Is it possible to list all the instances that a word appears in a Word 2000 document?

Example: Search (Find) Demineralized
Listing Results:

Ideal Display...

[tt]
Instance Page No.
-----------------------
Demineralized 10
demineralized 23
deMineralized 24
-----------------------
[/tt]

Second Option...

[tt]
Instance Count
-----------------------
Demineralized 10
demineralized 3
deMineralized 1
-----------------------
[/tt]

If Page No. or Count isn't available just knowing the variations would be sufficient.


Thanks.
 
You can play with the ActiveDocument.Words collection, something like this:
Option Compare Text
For Each w In ActiveDocument.Words
If w.Text Like "demineralized" Then
...
End If
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks for the information. I tried it, seems like it treats de-mineralized as two words and so can't match it. Also, tried adding a statement to display page number, but it seems to only display the last page. Any thoughts would be greatly appreciated.

Code:
Public Sub FindWords()
'Option Compare Text
    Dim stData As String
    stData = UCase(InputBox("Enter Word"))
    For Each w In ActiveDocument.Words
        If UCase(w.Text) = stData Then
        Debug.Print w.Text ' & " " & Selection.Information(wdActivecurrentPageNumber)
    End If
    Next
End Sub
 
Hi,

Here's something that I used many years ago to create an Index for every word in a document.

Copy the document to new concordance document.

Replace Space with ^p (hard return)

Delete any words you don't want

See Help on creating a Concordance

Create a macro for marking each index entry

Create the Index -- give you alpha list with page numbers

:)



Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
OK, here's a macro to mark the entries in the Concordance Table
Code:
Sub MarkConcordanceEntries()
    With ActiveDocument
        For Each r In .Tables(1).Rows
            s = Left(r.Range.Text, Len(r.Range.Text) - 3)
            .Indexes.MarkAllEntries _
                Range:=r.Range, _
                Entry:=s, _
                EntryAutoText:=s, _
                CrossReference:="", _
                CrossReferenceAutoText:="", _
                BookmarkName:="", _
                Bold:=False, _
                Italic:=False
        Next
    End With
End Sub
:)

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Should have some code to remove existing index references before adding new ones
Code:
Sub MarkConcordanceEntries()
    With ActiveDocument
        RemoveIndexEntries
        For Each r In .Tables(1).Rows
            s = Left(r.Range.Text, Len(r.Range.Text) - 3)
            .Indexes.MarkAllEntries _
                Range:=r.Range, _
                Entry:=s, _
                EntryAutoText:=s, _
                CrossReference:="", _
                CrossReferenceAutoText:="", _
                BookmarkName:="", _
                Bold:=False, _
                Italic:=False
        Next
    End With
End Sub
Sub RemoveIndexEntries()
    With ActiveDocument
        For Each f In .Fields
            With f
                If .Type = wdFieldIndexEntry Then .Delete
            End With
        Next
    End With
End Sub
:)


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Instead of this:
Debug.Print w.Text & " " & Selection.Information(wdActivecurrentPageNumber)
have you tried this ?
Debug.Print w.Text & " " & w.Information(wdActivecurrentPageNumber)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
When I tried
Debug.Print w.Text & " " & w.Information(wdActivecurrentPageNumber)

I got error message "Value out of Range". Highlighting the code showed that it was empty. Also, still would need to be able to display words with a dash (De-Mineralized).
 
Have you tried this ?
If Replace(w.Text,"-","") Like "demineralized" Then

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Unfortunateley, I'm not too familiar with Word VBA as most of my vba experience is with MS-Access. I tried your suggestion and it doesn't work as the word is already split before it can reach the code to remove dash. I noticed this happenning when I hover the w with the mouse in the code below...

For Each w In ActiveDocument.Words



 
And what about something like this ?
last1w = "": last2w = ""
Option Compare Text
For Each w In ActiveDocument.Words
If w.Text Like "demineralized" Or _
(w.Text Like "mineralized" And last2w Like "de" And last1w ="-") Then
...
End If
last2w = last1w
last1w = w.Text
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you PH that code worked and increased the flexibility as before wasn't sure how to use the like in word.

Now I just need to figure out how to get the page no. to work, the code seems only to be able to display the page number where the cursor is located (If cursor is on page 2 before running code, then all words show as page 2 and if on page 1 then all show as page 1).
 
Have you tried this ?
w.Information(wdActiveEndPageNumber)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
PHV,

Doesn't work. Only shows matched items that are on the last page, not all items in the document that match.
 
And what about something like this ?
last1w = "": last2w = ""
Option Compare Text
For Each w In ActiveDocument.Words
If w.Text Like "demineralized" Or _
(w.Text Like "mineralized" And last2w Like "de" And last1w ="-") Then
...
w.Select
Debug.Print Selection.Information(wdActiveEndPageNumber)
End If
last2w = last1w
last1w = w.Text
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks PHV. That fixed the page number issue. Now, for some reason when I placed the code into the document I actually want to check the words for I can't get the code to behave consistently. It only found 2 occurences even though there are 4 within the first 20 pages. Could this be due to the fact that the file was created by someone else and contains page breaks, tables, contents, indeces, etc? The file contains 366 pages and the page numbers on the pages are different due to intro pages so that the last page displays 333 while its actual number is 366.
 
You have to play with the StoryRanges collection:
For Each aStory In ActiveDocument.StoryRanges
For Each w In aStory.Words
...
Next w
Next aStory
For the Page number issue, you may have to call the PrintPreview method to force Word paginate the document.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I tried your suggestion, and still only returns 2 instances rather than the expected 20+ instances. Perhaps I didn't enter it in the correct way??

Code:
Public Sub FindWords()
    last1w = "": last2w = ""
    Dim stWord As String
    Dim stData As String
    Dim intPage As Integer
    stData = InputBox("Enter Word")
    For Each aStory In ActiveDocument.StoryRanges
        For Each w In aStory.Words
            If w.Text Like stData Or _
              (w.Text Like stData And last2w Like "de" And last1w = "-") Then
                If last1w = "-" Then
                    stWord = last2w & last1w & w.Text
                Else
                    stWord = w.Text
                End If
                w.Select
                intPage = Selection.Information(wdActiveEndPageNumber)
                Debug.Print stWord & " " & intPage
            End If
            last2w = last1w
            last1w = w.Text
        Next w
    Next aStory
End Sub
 
Have you forgotten this ?
Option Compare Text

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I do have it. It is above the sub because when I had it inside the sub, received an error message "Invalid inside procedure"

Option Compare Text
Public Sub FindWords()
..
..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top