问题
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
回答1:
For Each sentence In ActiveDocument.StoryRanges
For Each w In sentence.Words
Debug.Print w
Next
Next
回答2:
Here's another, very similar solution which may be helpful for others. The accepted answer grabs truly all words in the document including header, footer, etc., whereas this answer will only grab 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
来源:https://stackoverflow.com/questions/22711120/how-to-loop-through-each-word-in-a-word-document-vba-macro