Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word VBA 2010 - Formatting the last paragraph in a table cell

Tags:

ms-word

vba

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?

like image 590
jarch3r Avatar asked Oct 25 '25 02:10

jarch3r


1 Answers

The "end of cell" marker is Chr(13) + Chr(7), so you can detect a paragraph located at the end of a cell using code similar to below:

Sub Tester()
Dim EOC As String
Dim p As Paragraph
Dim rng As Range

    EOC = Chr(13) & Chr(7)

    For Each p In ActiveDocument.Paragraphs

        If Len(p.Range.Text) > Len(EOC) And p.Range.Text Like "*" & EOC Then
               Set rng = p.Range

               'commenting out next line will select the whole cell
               rng.MoveEnd wdCharacter, -1  

               rng.Select
               MsgBox "Found paragraph at end of cell..."
        End If

    Next p

End Sub
like image 161
Tim Williams Avatar answered Oct 27 '25 18:10

Tim Williams