Thread: [Solved] Pull all lists from document
View Single Post
 
Old 01-30-2024, 10:54 PM
tonykekw tonykekw is offline Windows 11 Office 2021
Novice
 
Join Date: Jan 2024
Posts: 13
tonykekw is on a distinguished road
Post Pull all lists from document

VBA for MS Word
trying to extract all lists with their respected data.
a) list type
b) list Content
c) heading level

this data gets added to a new word doc for the time being.
I am getting stuck on how to go about pulling the lists. ( I use the bult in MS Word list types). Eventually I would like to pull the data and move it to an excel sheet.

Code:
Sub ExtractAllLists()
    Dim aRng As Range
    Dim aRngHeader As Range
    Dim aDoc As Document
    Dim aDocNew As Document
    Dim aTbl As Table
    Dim aRow As Row
    Dim sNum As String

    Set aDoc = ActiveDocument
    Set aRng = aDoc.Range
    Set aDocNew = Documents.Add
    Set aTbl = aDocNew.Tables.Add(aDocNew.Range, 1, 3)
    aTbl.Cell(1, 1).Range.Text = "List Type"
    aTbl.Cell(1, 2).Range.Text = "List Contents"
    aTbl.Cell(1, 3).Range.Text = "Heading"

    With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Forward = True
        Do While .Execute
            aRng.Start = aRng.Paragraphs(1).Range.Start
            Set aRow = aTbl.Rows.Add
            If aRng.ListFormat.ListType <> wdListNoNumbering Then  
                aRow.Cells(2).Range.FormattedText = aRng.FormattedText
            Else
                aRow.Cells(2).Range.Text = aRng.Text
            End If
                ' stuck here




    ' Retrieve heading information
    Set aRngHead = aRng.GoToPrevious(wdGoToHeading)
    aRngHead.End = aRngHead.Paragraphs(1).Range.End - 1
    aRow.Cells(3).Range.Text = aRngHead.ListFormat.listString & vbTab & aRngHead.Text
    
    ' Move to the end of the range
    aRng.Collapse Direction:=wdCollapseEnd
    aRng.End = aDoc.Range.End
practicing VBA everyday, if anyone has suggestions on learning VBA let me know, i dont want to ask to many Q. or spam help. thanks
Reply With Quote