4

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
braX
  • 11,506
  • 5
  • 20
  • 33
Kamran
  • 4,010
  • 14
  • 60
  • 112

2 Answers2

8
For Each sentence In ActiveDocument.StoryRanges
    For Each w In sentence.Words
        Debug.Print w
    Next
Next
Aidan
  • 1,550
  • 1
  • 13
  • 20
2

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
Marcucciboy2
  • 3,156
  • 3
  • 20
  • 38