Word Sentences collection skipping text

A

Alfontz

Thank you in advance for assistance with my issue.

I have some code that enumerates the Sentences collection searching for
specific text. With the following paragraph, the Sentences collection skips
the first half of the stentence.

To reproduce the issue.
Create a new document
Add the following text:

"How many sentences (e.g., ranges) are in this paragraph?"

Create a simple macro:
Sub GetSentences()
Dim r As Range
Dim s As Range

Set r = ActiveDocument.Content

For Each s In r.Sentences
MsgBox s.Text
Next s
End Sub

Notice that the Sentences collection has 2 items?
","
", ranges) are in this paragraph?"

Expected result is only 1 item in the Sentences collection.

Thanks,
Al
 
T

Tony Jollans

To reproduce the issue.

That is a sentence; it ends in a full stop.
Create a new document
Add the following text:

That is deemed a sentence, ending at the end of the document.

What is the one sentence you were expecting? And what 'first half' do you
not see?
 
M

macropod

Hi Alfontz,

I agree that's not a good result. Here's a workaround:

Sub GetSentences()
Dim p As Paragraph, q As Range, r As Range, s As Range, i As Integer
For Each p In ActiveDocument.Paragraphs
Set q = p.Range
i = q.ComputeStatistics(wdStatisticWords)
If q.Characters.Count > 1 Then
Set r = q.Words.First
Set s = q.Words.First
With s
Do
.Expand wdSentence
.MoveEndWhile cset:=vbCr, Count:=wdBackward
r.MoveEnd wdSentence, 1
r.MoveEndWhile cset:=vbCr, Count:=wdBackward
MsgBox .Text
.Collapse wdCollapseEnd
Loop Until r.ComputeStatistics(wdStatisticWords) = i
End With
End If
Next p
Set p = Nothing
Set q = Nothing
Set r = Nothing
Set s = Nothing
End Sub

Do note, though, that Word has a strange idea of what a sentence is anyway - it can, for example, include a sentence and any number
of subsequent empty paragraphs!
 
M

macropod

Hi Tony,

If the document contains:
"How many sentences (e.g., ranges) are in this paragraph?"
Alfontz's macro ignores:
"How many sentences (e.g."
That shouldn't be, regardless of Word vba's inability to determine what constitutes a 'real' sentence. It'd be nice if vba had
access to the same 'sentence' logic that the UI grammar checker uses.
 
T

Tony Jollans

<blush> I copletely misread that post! And I do agree that's a pretty
strange result.

--
Enjoy,
Tony

www.WordArticles.com

macropod said:
Hi Tony,

If the document contains:
"How many sentences (e.g., ranges) are in this paragraph?"
Alfontz's macro ignores:
"How many sentences (e.g."
That shouldn't be, regardless of Word vba's inability to determine what
constitutes a 'real' sentence. It'd be nice if vba had access to the same
'sentence' logic that the UI grammar checker uses.

--
Cheers
macropod
[Microsoft MVP - Word]


Tony Jollans said:
That is a sentence; it ends in a full stop.


That is deemed a sentence, ending at the end of the document.

What is the one sentence you were expecting? And what 'first half' do you
not see?

--
Enjoy,
Tony

www.WordArticles.com
 
A

Alfontz

macropod,

Thanks for the workaround and the timely responce. Should be good enough
for now.

Al

macropod said:
Hi Alfontz,

I agree that's not a good result. Here's a workaround:

Sub GetSentences()
Dim p As Paragraph, q As Range, r As Range, s As Range, i As Integer
For Each p In ActiveDocument.Paragraphs
Set q = p.Range
i = q.ComputeStatistics(wdStatisticWords)
If q.Characters.Count > 1 Then
Set r = q.Words.First
Set s = q.Words.First
With s
Do
.Expand wdSentence
.MoveEndWhile cset:=vbCr, Count:=wdBackward
r.MoveEnd wdSentence, 1
r.MoveEndWhile cset:=vbCr, Count:=wdBackward
MsgBox .Text
.Collapse wdCollapseEnd
Loop Until r.ComputeStatistics(wdStatisticWords) = i
End With
End If
Next p
Set p = Nothing
Set q = Nothing
Set r = Nothing
Set s = Nothing
End Sub

Do note, though, that Word has a strange idea of what a sentence is anyway - it can, for example, include a sentence and any number
of subsequent empty paragraphs!

--
Cheers
macropod
[Microsoft MVP - Word]


Alfontz said:
Thank you in advance for assistance with my issue.

I have some code that enumerates the Sentences collection searching for
specific text. With the following paragraph, the Sentences collection skips
the first half of the stentence.

To reproduce the issue.
Create a new document
Add the following text:

"How many sentences (e.g., ranges) are in this paragraph?"

Create a simple macro:
Sub GetSentences()
Dim r As Range
Dim s As Range

Set r = ActiveDocument.Content

For Each s In r.Sentences
MsgBox s.Text
Next s
End Sub

Notice that the Sentences collection has 2 items?
","
", ranges) are in this paragraph?"

Expected result is only 1 item in the Sentences collection.

Thanks,
Al
 

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