Current sentence

S

Sam Hobbs

I need to process each sentence beginning with the current sentence. That
sounds easy to do, and I assume it is, but I cannot find how. I have tried
to look in the documentation and I have searched this newsgroup.

I know about the Sentences collection and how to use it.

I have read in the documentation about the insertion point but it is not
clear how to get the location of it. I assume that if I have the index in
the Characters collection of the insertion point then that can be converted
to the index of sentence in the Sentences collection, but if anyone can help
with any of that then I hope to be able to figure out the rest.

I can read the documentation and such, but I need help finding what to read.

I found "Determine the index number of the current paragraph, table, section
...." at:
http://word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm

Which does what I need done exept I need to do it for sentences but I can't
get it to work for sentences.
 
J

Jay Freedman

I need to process each sentence beginning with the current sentence. That
sounds easy to do, and I assume it is, but I cannot find how. I have tried
to look in the documentation and I have searched this newsgroup.

I know about the Sentences collection and how to use it.

I have read in the documentation about the insertion point but it is not
clear how to get the location of it. I assume that if I have the index in
the Characters collection of the insertion point then that can be converted
to the index of sentence in the Sentences collection, but if anyone can help
with any of that then I hope to be able to figure out the rest.

I can read the documentation and such, but I need help finding what to read.

I found "Determine the index number of the current paragraph, table, section
..." at:
http://word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm

Which does what I need done exept I need to do it for sentences but I can't
get it to work for sentences.

The current insertion point or extended selection is represented by the
Selection object. The Selection object has a Sentences collection that is
(usually) different from the document's Sentences collection. The expression

Selection.Sentences(1)

returns the first sentence in the Selection (NOT in the document). That's true
even if the Selection represents a collapsed insertion point, in which case the
returned sentence is the one that contains the insertion point.

Because the members of any Sentences collection are Range objects, you can
assign one to a Range object and then work with that. In particular, you can
reset the end of the Range object to the end of the document -- so the Range now
goes from the beginning of the sentence from the selection, to the end of the
document. That Range object in turn has its own Sentences collection, and you
can use a For Each loop to iterate through it.

For a silly example, this demo changes the color of each sentence from the one
containing the insertion point to the end of the document.

Sub demo()
Dim myColor As WdColorIndex
myColor = wdBlue

Dim myRange As Range
Dim mySent As Range

Set myRange = Selection.Sentences(1)
myRange.End = ActiveDocument.Range.End

For Each mySent In myRange.Sentences
mySent.Font.ColorIndex = myColor
myColor = myColor + 1
If myColor > 16 Then myColor = wdBlue
Next
End Sub

Note that _nowhere_ in this code do you know or care about the absolute index
number of the current sentence.
 
S

Sam Hobbs

Jay Freedman said:
The current insertion point or extended selection is represented by the
Selection object. The Selection object has a Sentences collection that is
(usually) different from the document's Sentences collection. The
expression

Selection.Sentences(1)

returns the first sentence in the Selection (NOT in the document). That's
true
even if the Selection represents a collapsed insertion point, in which
case the
returned sentence is the one that contains the insertion point.

Because the members of any Sentences collection are Range objects, you can
assign one to a Range object and then work with that. In particular, you
can
reset the end of the Range object to the end of the document -- so the
Range now
goes from the beginning of the sentence from the selection, to the end of
the
document. That Range object in turn has its own Sentences collection, and
you
can use a For Each loop to iterate through it.

For a silly example, this demo changes the color of each sentence from the
one
containing the insertion point to the end of the document.

Sub demo()
Dim myColor As WdColorIndex
myColor = wdBlue

Dim myRange As Range
Dim mySent As Range

Set myRange = Selection.Sentences(1)
myRange.End = ActiveDocument.Range.End

For Each mySent In myRange.Sentences
mySent.Font.ColorIndex = myColor
myColor = myColor + 1
If myColor > 16 Then myColor = wdBlue
Next
End Sub

Note that _nowhere_ in this code do you know or care about the absolute
index
number of the current sentence.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.


Thank you. That helps; it is not the entire solution, but it is enough for
me to figure out the rest.

I said "all" sentences. So after going to the bottom, I need to restart from
the beginning and then go to the sentence preceding the current (starting)
location, without processing the starting sentence twice. So I can use your
sample, exept I need to use the ranges as arrays (or whatever terminology
applies) instead of collections. So the following seems to work; should it?

Dim BottomRange As Range
Dim TopRange As Range
Dim SentenceIndex As Long
Text1 = ""
Set BottomRange = Selection.Sentences(1)
BottomRange.End = ActiveDocument.Range.End
For SentenceIndex = 1 To BottomRange.Sentences.Count
Text1 = Text1 + BottomRange.Sentences(SentenceIndex) + vbCrLf
Next
Text1 = Text1 + vbCrLf
Set TopRange = Selection.Sentences(1)
TopRange.Start = ActiveDocument.Range.Start
For SentenceIndex = 1 To TopRange.Sentences.Count - 1
Text1 = Text1 + TopRange.Sentences(SentenceIndex) + vbCrLf
Next
 
J

Jay Freedman

Sam said:
Thank you. That helps; it is not the entire solution, but it is
enough for me to figure out the rest.

I said "all" sentences. So after going to the bottom, I need to
restart from the beginning and then go to the sentence preceding the
current (starting) location, without processing the starting sentence
twice. So I can use your sample, exept I need to use the ranges as
arrays (or whatever terminology applies) instead of collections. So
the following seems to work; should it?
Dim BottomRange As Range
Dim TopRange As Range
Dim SentenceIndex As Long
Text1 = ""
Set BottomRange = Selection.Sentences(1)
BottomRange.End = ActiveDocument.Range.End
For SentenceIndex = 1 To BottomRange.Sentences.Count
Text1 = Text1 + BottomRange.Sentences(SentenceIndex) + vbCrLf
Next
Text1 = Text1 + vbCrLf
Set TopRange = Selection.Sentences(1)
TopRange.Start = ActiveDocument.Range.Start
For SentenceIndex = 1 To TopRange.Sentences.Count - 1
Text1 = Text1 + TopRange.Sentences(SentenceIndex) + vbCrLf
Next

Yes, that should work perfectly. Just to be sure, test it in the "edge
cases":

- The Selection is in the first sentence of a multiple-sentence document.
- The Selection is in the last sentence of a multiple-sentence document.
- The Selection contains more than one sentence.
- The Selection contains two or more unconnected pieces (by holding Ctrl
while selecting the next piece). VBA should ignore all but the first piece
(see http://support.microsoft.com/kb/288424).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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