using for each to select sentences in a cell

A

armsy

Hi,

I have managed to find a work around to my current problem but I am
intrigued as to why this is happening.

Word 2003

I am extracting individual lines out of a Table cell and placing them
in a collection which i am placing in a user form to provide a user
interface.

Previously i would have iterated over the string until i found a
carriage return / line feed character but after discovering the joys
of the Word Range object (thanks to the contributions in this group!)
I thought there must be a much more elegant solution.

Which i thought would be along the lines of
 
A

armsy

how embarrassing i accidentally posted this before completing the
question. many apologies!


Hi,

I have managed to find a work around to my current problem but I am
intrigued as to why this is happening.

Word 2003

I am extracting individual lines out of a Table cell and placing them
in a collection which i am placing in a user form to provide a user
interface.

Previously i would have iterated over the string until i found a
carriage return / line feed character but after discovering the joys
of the Word Range object (thanks to the contributions in this group!)
I thought there must be a much more elegant solution.

Which i thought would be along the lines of

set rngCell = Selection.Cells(1).Range

With rngCell

for each var in .Sentences
myCollection.add var
next
End WIth

However, it soon became apparent that this method was not picking up
the last sentence in the cell. I was aware that i might be picking up
the end of cell marker, so i tried out several ways of redefining the
range object with .end - 2 etc but to no avail, I finally achievd my
goal by adding the following line outside the With statement

var =
ActiveDocument.Range(.Sentences.Last.Start, .Sentences.Last.End - 2)
myCollection.add var

However, feels very clunky, is there anything i should have done to
acheive this within the for each?

Thanks in advance

Simon

PS Sorry again about the premature post can the tab order be changed
to go to a preview rather than Send? ;o$
 
D

Doug Robbins - Word MVP

An alternative (not necessarily superior) would be to use:

Dim mcell As Range
Dim i As Long
Set mcell = Selection.Cells(1).Range
With mcell
.End = .End - 2
For i = 1 To .Sentences.Count
MsgBox Left(.Sentences(i), InStr(.Sentences(i), "."))
Next
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

armsy

An alternative (not necessarily superior) would be to use:

Dim mcell As Range
Dim i As Long
Set mcell = Selection.Cells(1).Range
With mcell
    .End = .End - 2
    For i = 1 To .Sentences.Count
        MsgBox Left(.Sentences(i), InStr(.Sentences(i), "."))
    Next
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

















- Show quoted text -

it does look neater thanks Doug, however I am no closer to
understanding why the 'for each ... next' construct did not fully
iterate through the sentences collection?
 
D

Doug Robbins - Word MVP

Actually, the .End = .End - 2 in my code was not needed, as it did not do
the intended job of stripping off the end of cell marker from the last
sentence, hence the use of the Left(.Sentences(i), InStr(.Sentences(i),
".")) construction to eliminate it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

An alternative (not necessarily superior) would be to use:

Dim mcell As Range
Dim i As Long
Set mcell = Selection.Cells(1).Range
With mcell
.End = .End - 2
For i = 1 To .Sentences.Count
MsgBox Left(.Sentences(i), InStr(.Sentences(i), "."))
Next
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

















- Show quoted text -

it does look neater thanks Doug, however I am no closer to
understanding why the 'for each ... next' construct did not fully
iterate through the sentences collection?
 
A

armsy

Actually, the .End = .End - 2 in my code was not needed, as it did not do
the intended job of stripping off the end of cell marker from the last
sentence, hence the use of the Left(.Sentences(i), InStr(.Sentences(i),
".")) construction to eliminate it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








it does look neater thanks Doug, however I am no closer to
understanding why the 'for each ... next' construct did not fully
iterate through the sentences collection?- Hide quoted text -

- Show quoted text -

something strange is going on! I modified your routine to remove the
InStr construction as I can't rely on my users to be precise enough to
use a "." to terminate the line.

just debugging through the loop.

rngCell has the value of "Text""Text""Text" if you hover over it

but when i iterate through the sentences collection as suggested

.Sentence[1] = "Text"
.Sentence[2] = "Text"
.Sentence[3] = "Text"

where did all those extra control characters come from?

i'm thinking that i might use the for loop to retrieve the individual
sentences but parse that sentence through an additional function to
strip out any control characters.
 
D

Doug Robbins - Word MVP

As there is no Sentence object in VBA. I am not really sure what really
defines the individual sentences in the VBA Sentences object with is a
"collection of Range objects that represent all the sentences in a
selection, range, or document."

Based on what I learnt at school about 55+ years ago, I assume (though have
not tested to check) that a Sentence is a Range that terminates in a period.
That at school, I also learnt that it had a subject, a verb and an object.
However I realise that they don't teach grammar like they used to, so the
definition might now be somewhat different.

Are you actually dealing with sentences in the cell or are they individual
paragraphs. That is those other than the last terminate with a carriage
return (¶)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Actually, the .End = .End - 2 in my code was not needed, as it did not do
the intended job of stripping off the end of cell marker from the last
sentence, hence the use of the Left(.Sentences(i), InStr(.Sentences(i),
".")) construction to eliminate it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








it does look neater thanks Doug, however I am no closer to
understanding why the 'for each ... next' construct did not fully
iterate through the sentences collection?- Hide quoted text -

- Show quoted text -

something strange is going on! I modified your routine to remove the
InStr construction as I can't rely on my users to be precise enough to
use a "." to terminate the line.

just debugging through the loop.

rngCell has the value of "Text""Text""Text" if you hover over it

but when i iterate through the sentences collection as suggested

..Sentence[1] = "Text"
..Sentence[2] = "Text"
..Sentence[3] = "Text"

where did all those extra control characters come from?

i'm thinking that i might use the for loop to retrieve the individual
sentences but parse that sentence through an additional function to
strip out any control characters.
 
A

armsy

- Show quoted text -

Yes I was wondering what the Word definition of a sentence was too.
Primarily i am dealing with lines of text finalised with a paragraph
mark, within a table cell.

However your example using the full stop (period in US) as the
delimiter within the InStr made me reconsider and experiment with
placing a full stop at the end of each line. However, I discovered
that if I had only a single line of text in a cell terminated with a
full stop or a paragraph mark, when I iterated through the sentence
collection, the sentence count was 1, however all it contained were
two control characters which I presume is the end of cell marker, yet
this control characters are not at the end of the range object itself?

At this point I returned to the old fashioned method of interrogating
the string for a paragraph mark and stripping out the contents line by
line.

Thanks for your time though Doug, its been a intersting learning
experience something I will get back to once i've got my boss of my
back to finish this current project!

Simon
 
D

Doug Robbins - Word MVP

I would then use the Paragraph object

Dim lastpara As Range
Dim i As Long
With Selection.Cells(1).Range
For i = 1 To .Paragraphs.Count - 1
MsgBox .Paragraphs(i).Range.Text
Next i
Set lastpara = .Paragraphs(i).Range
lastpara.End = lastpara.End - 1
MsgBox lastpara.Text
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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