Kamran
Kamran

Reputation: 4100

Loop through each word in a word document

I know how to get every paragraph in a word document. But I am looking for a way to loop through each word in a MS Word document.

Sub Sample()

Dim apara As Paragraph
Dim lineText As String

For Each apara In ActiveDocument.Paragraphs

     lineText = apara.Range

     'Now print the paragraph 

     Debug.Print lineText 

Next apara

End Sub

Upvotes: 4

Views: 22262

Answers (2)

Marcucciboy2
Marcucciboy2

Reputation: 3263

Here's another, very similar solution which may be helpful for others. The accepted answer grabs every single word in the document including header, footer, etc., whereas this answer will only grab the words in the "main" area of the document.

For Each para In ActiveDocument.Paragraphs
    For Each wrd In para.Range.Words
        Debug.Print wrd
    Next wrd
Next para

Upvotes: 4

Aidan
Aidan

Reputation: 1750

For Each sentence In ActiveDocument.StoryRanges
    For Each w In sentence.Words
        Debug.Print w
    Next
Next

Upvotes: 8

Related Questions