Selecting Last Sentence in a Table

S

Steve

I'm having trouble selecting sentences in tables (I want to select the last
sentence). Suppose there are three sentences in a cell. Table has 1 row and
1 column for illustration:

Sub SelectSentences()
Dim i As Integer

i = ActiveDocument.Tables(1).Rows(1).Range.Sentences.Count
ActiveDocument.Tables(1).Rows(1).Range.Sentences(i).Select '(Selects
the entire row, i.e., all three sentences!)

ActiveDocument.Tables(1).Rows(1).Range.Sentences(i - 1).Select
'(Selects sentence 2)

ActiveDocument.Tables(1).Rows(1).Range.Sentences.Last.Select
'(Selects the end-of-row marker)

End Sub

Argh!! How in the world do I select ONLY the LAST SENTENCE???

Thanks. Steve
 
S

Steve

Thanks, Mark. I have a single-cell table. The one cell has three sentences.
I need to SELECT the last sentence. I'll try working with your suggestion.

Steve


Mark Baird said:
The data in the three cells is actually three separate paragraphs.

You have to specify the cell in which you want to get data.

Dim sString As String

sString = ActiveDocument.Tables(1).Cell(1, 3).Range.Text
' The last two characters on non-printable characters.
sString = Left(sString, Len(sString) - 2)

Mark Baird

I'm having trouble selecting sentences in tables (I want to select the last
sentence). Suppose there are three sentences in a cell. Table has 1 row and
1 column for illustration:

Sub SelectSentences()
Dim i As Integer

i = ActiveDocument.Tables(1).Rows(1).Range.Sentences.Count
ActiveDocument.Tables(1).Rows(1).Range.Sentences(i).Select '(Selects
the entire row, i.e., all three sentences!)

ActiveDocument.Tables(1).Rows(1).Range.Sentences(i - 1).Select
'(Selects sentence 2)

ActiveDocument.Tables(1).Rows(1).Range.Sentences.Last.Select
'(Selects the end-of-row marker)

End Sub

Argh!! How in the world do I select ONLY the LAST SENTENCE???

Thanks. Steve
 
M

Mark Baird

Following is an example of how to get the last sentence or the last line of text in the cell

Mark Bair

Dim sString As Strin

'Get the last sentence
sString = ActiveDocument.Tables(1).Cell(1, 1).Range.Sentences.Last.Tex
' Trim off the end of cell marker and paragraph mark
sString = Left(sString, Len(sString) - 2

' Select the last character in the cell
ActiveDocument.Tables(1).Cell(1, 1).Range.Characters(ActiveDocument.Tables(1).Cell(1, 1).Range.Characters.Count - 1).Selec
' Get the line of text that the selection is in. "\Line" is a predefined bookmark in Word at relates to current cursor position
sString = Selection.Bookmarks("\Line").Range.Tex
' Trim off the end of cell marker and paragraph mark
sString = Left(sString, Len(sString) - 2
 
S

Steve

Mark...still no good, but thanks for helping. I need to SELECT the last
sentence in a cell, not get its text.

Your "Bookmarks("\Line") approach still selects all the sentences in the
cell (I never knew this bookmark existed. Is this documented somewhere?)

Also, I find that there are four characters at the end of the cell that need
to be stripped off (Win 2000, Word 2000).

It's interesting that these two lines of code deliver very different
results, yet they have the same syntax:
(1) ActiveDocument.Tables(1).Cell(1, 1).Range.Sentences.Last.Select
(2) sString = ActiveDocument.Tables(1).Cell(1, 1).Range.Sentences.Last.Text

(1) selects all sentences in the cell, while (2) gets the text of the last
sentence ONLY plus the trailing four characters.
Why this difference in behavior?

Steve


Mark Baird said:
Following is an example of how to get the last sentence or the last line of text in the cell.


Mark Baird

Dim sString As String

'Get the last sentence.
sString = ActiveDocument.Tables(1).Cell(1, 1).Range.Sentences.Last.Text
' Trim off the end of cell marker and paragraph mark.
sString = Left(sString, Len(sString) - 2)

' Select the last character in the cell.
ActiveDocument.Tables(1).Cell(1,
1).Range.Characters(ActiveDocument.Tables(1).Cell(1,
1).Range.Characters.Count - 1).Select
' Get the line of text that the selection is in. "\Line" is a
predefined bookmark in Word at relates to current cursor position.
 
S

Steve

Mark...Here's a simple solution:

Dim oCell2 As Cell ' Second cell in table
Dim lStart As Long ' For finding beginning of last sentence in
Table caption
Dim lEnd As Long ' For finding ending of last sentence in Table
caption

lStart = oCell2.Range.Sentences.Last.Start ' Find start of last
sentence.
lEnd = oCell2.Range.Sentences.Last.End - 2 ' Find end of last
sentence
ActiveDocument.Range(Start:=lStart, End:=lEnd).Select ' Can't look
in the table because lStart & lEnd are global
' positions from the beginning of the doc.

Steve
 
P

Peter Hewett

Hi Steve

Try something like this it should do what you want:

Public Sub SelectSentence(ByVal lngSentence As Long)
Dim rngSentence As Word.Range

With ActiveDocument.Tables(1).Cell(1, 1).Range
If lngSentence >= 1 And lngSentence <= .Sentences.Count Then
If lngSentence = .Sentences.Count Then
Set rngSentence = .Sentences(lngSentence)
rngSentence.End = .End - 2
rngSentence.Select
Else
.Sentences(lngSentence).Select
End If
End If
End With
End Sub

Call it like this:
SelectSentence 3

The "\Line" is one of sixteen predefined bookmarks. You can find them in the VBA help
(use the keywords "Predefined Bookmarks").

The difference in behavior is due to the fact that you are selecting the end of cell
marker, whenever you select the end of cell marker it *always* selects the entire cell
contents. This behaviors is why the above code has two paths. One for any sentence other
than the last in the cell and the other for the last sentence in the cell.

HTH + Cheers - Peter
 
S

Steve

Peter...nice solution. I worked out this 3-line solution a little while ago.
Seems to work fine on all the corner-cases I can throw at it (multiple
lines; no characters; one sentence without period; etc)
Thanks for your help.

Sub SelectLastSentence()
Dim oTbl As Table
Dim oCell2 As Cell ' Second cell in table
Dim lStart As Long ' For finding beginning of last sentence in
Table Caption
Dim lEnd As Long ' For finding ending of last sentence in Table
Caption

Set oTbl = ActiveDocument.Tables(1)
Set oCell2 = oTbl.Range.Cells(2)

lStart = oCell2.Range.Sentences.Last.Start ' Find start of last
sentence.
lEnd = oCell2.Range.Sentences.Last.End - 2 ' Find end of last
sentence
ActiveDocument.Range(Start:=lStart, End:=lEnd).Select ' Select the
last sentence
End Sub

Steve
 
M

Mac

See what ya mean, Steve.

This workaround did the job for me - execute your code
until you select the second last sentence (in your
example, sentence 2) and then include the following lines
of code:

Selection.MoveRight Unit:=wdSentence, Count:=1
Selection.MoveRight Unit:=wdSentence, Extend:=wdSentence

Regards

Mac
 

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