Search code examples
vbams-wordsentence

vba Word how to get sentences in a table


I have a Word document, with the first two sentences as normal text and sentences three and four in a table (in one cell):

My first sentence. My second sentence.

My third sentence. My fourth sentence.

My code is the following:

Option Explicit
Sub test()
    Dim sentence As Variant
    Dim i As Long: i = 0
    
    Selection.Expand wdSentence
    Debug.Print "--------->" & ActiveDocument.ActiveWindow.Selection.Sentences.Count
    Debug.Print "selection: " & ActiveDocument.ActiveWindow.Selection
    For Each sentence In ActiveDocument.ActiveWindow.Selection.Sentences
        i = i + 1
        Debug.Print i & " sentence: " & sentence
    Next

End Sub

If I select the first two sentences, the debug output is correct:

--------->2
selection: My first sentence. My second sentence.


1 sentence: My first sentence. 
2 sentence: My second sentence.

If I select the two sentences in the table, the debug output is strange (or wrong?):

--------->2
selection: My third sentence. My fourth sentence.

1 sentence: My third sentence. 

Why is the output of the table content different from normal text? How can I get the same result for the table content as for the normal text?


Solution

  • Tables bring a another whole dimension of complexity to what Word recognizes as a sentence. Paragraph, end of cell marks, and end of row markers all factor into the confusion of what constitutes a sentence to VBA.

    Here is some code that should work, but I can’t claim with 100% certainty that it will work in all situations. In other words, I know it can be improved upon, but it should give you a good start for your own debugging sessions.

    Sub ParseBySentence()
        Dim doc As Word.Document
        Dim i As Long, s As Long, para As Word.Paragraph
        Dim rng As Word.Range, sRng As Word.Range
        
        Application.ScreenUpdating = False
        On Error Resume Next
        Set doc = ActiveDocument
        For i = 1 To doc.Paragraphs.Count
            Set para = doc.Paragraphs(i)
            If para.Range.Information(wdWithInTable) Then
                Set rng = para.Range
                Do While Asc(rng.Characters.Last) = 13
                    rng.MoveEnd unit:=wdCharacter, Count:=-1
                Loop
                If rng.Text = vbNullString Or _
                    Asc(rng.Text) = 13 Then
                    'do nothing
                Else
                    For s = 1 To rng.Sentences.Count
                        Set sRng = rng.Sentences(s)
                        Do While Asc(sRng.Characters.Last) = 13
                            sRng.MoveEnd unit:=wdCharacter, Count:=-1
                        Loop
                        sRng.Select
                        Debug.Print Selection.Text
                        Selection.Collapse Word.WdCollapseDirection.wdCollapseEnd
                    Next
                End If
            End If
        Next
        Selection.HomeKey unit:=wdStory
        Application.ScreenUpdating = True
        MsgBox "Action Complete"
    End Sub