Forum Discussion
Gilgamesh1964
Oct 26, 2022Brass Contributor
How would I extract paragraph/list numbers and the paragraph text in VBA
Hello, I want like to have a VBA macro that will set a string for the paragraph/list number for every paragraph in the document along with a string for the text. For paragraphs that do not have a nu...
- Oct 26, 2022
You should be able to use the ListString property to retrieve the number format for each level. See https://wordmvp.com/FAQs/Numbering/ListString.htm.
Oct 26, 2022
Gilgamesh1964 Run a macro containing the following code when you have the document open
Dim docsource As Document, doctarget As Document
Dim i As Long
Dim tbl As Table
Dim newrow As Row
Set docsource = ActiveDocument
Set doctarget = Documents.Add
With doctarget
Set tbl = .Tables.Add(Selection.Range, 1, 2)
With tbl
.Cell(1, 1).Range.Text = "String 1"
.Cell(1, 2).Range.Text = "String 2"
End With
End With
With docsource
.ConvertNumbersToText
For i = 1 To .Paragraphs.Count
Set newrow = tbl.Rows.Add
If Left(.Paragraphs(i).Style, 7) = "Heading" Then
With newrow
.Cells(1).Range.Text = Left(docsource.Paragraphs(i).Range.Text, InStr(docsource.Paragraphs(i).Range.Text, vbTab) - 1)
.Cells(2).Range.Text = Mid(docsource.Paragraphs(i).Range.Text, InStr(docsource.Paragraphs(i).Range.Text, vbTab) + 1)
End With
Else
newrow.Cells(2).Range.Text = .Paragraphs(i).Range.Text
End If
Next i
.Close wdDoNotSaveChanges
End With
doctarget.Activate