'Word VBA 2010 - Formatting the last paragraph in a table cell
I have a project that I export from MadCap Flare into Word 2010 and I use a VBA script to update the formatting of the document. I am trying to check the style of every paragraph in the document, then if it matches a specific style apply a multi list level format.
It almost works problem-free. The problem arises when the paragraph falls as the last paragraph in a table cell. In this case, the range includes the end of cell marker (so the range includes every paragraph of the cell) and thus the change applies to every paragraph in the table cell instead of simply the last.
The code I use is as follows:
For Each iPara In ActiveDocument.Paragraphs
With iPara.Range
If iPara.Style.NameLocal = "div_NoteText" Then
.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord10ListBehavior, ApplyLevel:=1
End If
End With
Next
What changes do I need to make for this to work for the last paragraph in a table cell?
Solution 1:[1]
This program will check all paragraphs by first scanning all paragraphs that are not in a table, then checking all tables only applying changes to the last paragraph in each cell.
CheckParagraphs
Sub CheckParagraphs()
For Each iPara In ActiveDocument.Paragraphs
With iPara.Range
If Selection.Information(wdWithInTable) = False Then
If iPara.Style.NameLocal = "div_NoteText" Then
.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord10ListBehavior, ApplyLevel:=1
End If
End If
End With
Next
CheckTables
End Sub
CheckTables
Sub CheckTables()
Dim oPara As Range
Dim count As Integer
For Each t In ActiveDocument.Tables
For Each r In t.Rows
For Each c In r.Cells
With c.Range
'Only act on the last paragraph
With .Paragraphs(.Paragraphs.count).Range
If .Style.NameLocal = "div_NoteText" Then
.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord10ListBehavior
.SetListLevel Level:=1
End If
End With
End With
Next
Next
Next
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dotNET |
