Parsing sentences in a table

K

Ken

I want to look at sentences in table cells. The following code works
provided the cell is not the last in the row:

Set rTable = ThisDocument.Tables(1)
Set rnge = rTable.Rows(i).Cells(j).Range
For Each sentx in rnge.Sentences
Msgbox sentx.Text
Next sentx

If the cell is the last in the row, then the last sentence in the cell
is not shown, only the end of cell marker is displayed.

How do I get the last sentence in the last cell of a row?
 
P

Pesach Shelnitz

Hi Ken,

I ran your code in a macro and saw the same thing. I also saw something else
that you didn't mention. In the cells where the last sentence was displayed,
it was followed by a bullet symbol on a new line.

Let's focus on my second observation first. After some playing around, I
came to the conclusion that a sentence (an element in the Sentences
collection) is represented by a range (Range object) whose end is indicated
by the presence of sentence-terminating punctuation (for example, a period)
followed by white space. In the case of a table, the cell boundaries and row
boundaries are treated as white-space characters. Thus, the Range of the last
sentence in the cell at the end of a row includes two extra characters: one
that terminates the cell and one that terminates the row. In the cells that
are not at the end of a row, the Range of the last sentence includes only the
bullet-producing character that terminates the cell. In my opinion, all the
white-space characters between sentences should be not be included in the
sentences extracted from the table. By excluding them, the extra bullets are
eliminated.

Now let's discuss your original observation. For some reason, Word is
setting the boundaries of the Range of the last sentence in a cell at the end
of a row to include only the white space at its end. The solution to this
problem is to save the position of the start of the cell, set the starting
position of the Range for the first sentence in the cell to that position,
and then set the starting position of each additional sentence to the ending
position of the Range of the previous sentence.

The following macro includes all of these changes and works fine for me.

Sub ParseTableCells()
Dim rTable As table
Dim rnge As Range
Dim sentx As Range
Dim i, j, k As Integer
Dim pos As Integer

Set rTable = ActiveDocument.Tables(1)

For i = 1 To rTable.Rows.Count
For j = 1 To rTable.Columns.Count
Set rnge = rTable.Cell(i, j).Range

' Save the starting position of the cell.
pos = rnge.Start
For Each sentx In rnge.Sentences
sentx.Start = pos
pos = sentx.End
' Remove unwanted white space characters.
If j = rTable.Columns.Count Then
sentx.MoveEnd Unit:=wdCharacter, Count:=-2
Else
sentx.MoveEnd Unit:=wdCharacter, Count:=-1
End If
MsgBox sentx.Text
Next
Next
Next i
End Sub
 
K

Ken

Thank you Pesach.

Your method works fine but I think that I have found a slightly
simpler way.

The curious thing is that rnge contains all of the sentences and MsgBox
(rnge.Text) will list all of the text in the cell including the last
sentence. Also, rnge.Sentences.Count gives the correct number of
sentences.

So I moved the start of rnge to the end of the second last sentence
and thus rnge now contains only the last sentence. The last character
is the end of cell terminator which I remove with the MoveEnd command.

The resultant code is:

Sub ParseTableCells()
Dim rTable As Table
Dim rnge As Range
Dim scount As Integer
Set rTable = ThisDocument.Tables(1)
Set rnge = rTable.Rows(1).Cells(2).Range
scount = rnge.Sentences.Count
If scount > 1 Then rnge.Start = rnge.Sentences(scount - 1).End
rnge.MoveEnd Unit:=wdCharacter, Count:=-1
MsgBox rnge.Text
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top