Getting the headings from a Word document

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

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

7条回答
  •  萌比男神i
    2020-11-30 04:32

    Fastest method for extracting of all headings (to LEVEL5).

    Sub EXTRACT_HDNGS()
    Dim WDApp As Word.Application    'WORD APP
    Dim WDDoc As Word.Document       'WORD DOC
    
    Set WDApp = Word.Application
    Set WDDoc = WDApp.ActiveDocument
    
    For Head_n = 1 To 5
    Head = ("Heading " & Head_n)
    WDApp.Selection.HomeKey wdStory, wdMove
    
        Do
           With WDApp.selection
          .MoveStart Unit:=wdLine, Count:=1    
          .Collapse Direction:=wdCollapseEnd
           End with
            With WDApp.Selection.Find
              .ClearFormatting:          .text = "":     
              .MatchWildcards = False:   .Forward = True
              .Style = WDDoc.Styles(Head)
             If .Execute = False Then GoTo Level_exit
                .ClearFormatting
            End With
    
           Heading_txt = RemoveSpecialChar(WDApp.Selection.Range.text, 1):              Debug.Print Heading_txt
           Heading_lvl = WDApp.Selection.Range.ListFormat.ListLevelNumber:              Debug.Print Heading_lvl
           Heading_lne = WDDoc.Range(0, WDApp.Selection.Range.End).Paragraphs.Count:    Debug.Print Heading_lne
           Heading_pge = WDApp.Selection.Information(wdActiveEndPageNumber):            Debug.Print Heading_pge
    
           If Wdapp.Selection.Style = "Heading 1" Then GoTo Level_exit
           Wdapp.Selection.Collapse Direction:=wdCollapseStart
       Loop
    Level_exit:
    Next Head_n
    
    End Sub
    

提交回复
热议问题