How to loop through each word in a word document - VBA Macro

拥有回忆 提交于 2019-11-28 01:56:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!