Getting the headings from a Word document

后端 未结 7 1202
挽巷
挽巷 2020-11-30 04:11

How do I get a list of all the headings in a word document by using VBA?

7条回答
  •  鱼传尺愫
    2020-11-30 04:50

    The easiest way to get a list of headings, is to loop through the paragraphs in the document, for example:

     Sub ReadPara()
    
        Dim DocPara As Paragraph
    
        For Each DocPara In ActiveDocument.Paragraphs
    
         If Left(DocPara.Range.Style, Len("Heading")) = "Heading" Then
    
           Debug.Print DocPara.Range.Text
    
         End If
    
        Next
    
    
    End Sub
    

    By the way, I find it is a good idea to remove the final character of the paragraph range. Otherwise, if you send the string to a message box or a document, Word displays an extra control character. For example:

    Left(DocPara.Range.Text, len(DocPara.Range.Text)-1)
    

提交回复
热议问题