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

Re: using for each to select sentences in a cell by armsy

armsy
Tue Jul 15 08:16:41 PDT 2008

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$








Re: using for each to select sentences in a cell by Doug

Doug
Tue Jul 15 20:22:02 PDT 2008

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

"armsy" <sjdolding@gmail.com> wrote in message
news:bd4e3469-4507-4960-b694-16d1c6b6c53d@k30g2000hse.googlegroups.com...
> 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$
>
>
>
>
>
>
>



Re: using for each to select sentences in a cell by armsy

armsy
Wed Jul 16 00:45:45 PDT 2008

On Jul 16, 4:22=A0am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> An alternative (not necessarily superior) would be to use:
>
> Dim mcell As Range
> Dim i As Long
> Set mcell =3D Selection.Cells(1).Range
> With mcell
> =A0 =A0 .End =3D .End - 2
> =A0 =A0 For i =3D 1 To .Sentences.Count
> =A0 =A0 =A0 =A0 MsgBox Left(.Sentences(i), InStr(.Sentences(i), "."))
> =A0 =A0 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
>
> "armsy" <sjdold...@gmail.com> wrote in message
>
> news:bd4e3469-4507-4960-b694-16d1c6b6c53d@k30g2000hse.googlegroups.com...
>
>
>
> > 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
>
> > =A0 =A0 =A0 =A0 set rngCell =3D Selection.Cells(1).Range
>
> > =A0 =A0 =A0 =A0 With rngCell
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 for each var in .Sentences
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 myCollection.add var
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0next
> > =A0 =A0 =A0 =A0End WIth
>
> > However, it soon became apparent that this method was not picking up
> > the last sentence in the cell. =A0I was aware that i might be picking u=
p
> > the end of cell marker, so i tried out several ways of redefining the
> > range object with .end - 2 etc but to no avail, =A0I finally achievd my
> > goal by adding the following line outside the With statement
>
> > =A0 =A0 =A0 =A0var =3D
> > ActiveDocument.Range(.Sentences.Last.Start, .Sentences.Last.End - 2)
> > =A0 =A0 =A0 =A0myCollection.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$- Hide quoted text -
>
> - 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?

Re: using for each to select sentences in a cell by Doug

Doug
Wed Jul 16 01:35:32 PDT 2008

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

"armsy" <sjdolding@gmail.com> wrote in message
news:5f36aa90-f058-49d6-8df5-35a1552e2ba1@s50g2000hsb.googlegroups.com...
On Jul 16, 4:22 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> 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
>
> "armsy" <sjdold...@gmail.com> wrote in message
>
> news:bd4e3469-4507-4960-b694-16d1c6b6c53d@k30g2000hse.googlegroups.com...
>
>
>
> > 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$- Hide quoted text -
>
> - 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?



Re: using for each to select sentences in a cell by armsy

armsy
Wed Jul 16 02:44:27 PDT 2008

On Jul 16, 9:35=A0am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> Actually, the .End =3D .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
>
> "armsy" <sjdold...@gmail.com> wrote in message
>
> news:5f36aa90-f058-49d6-8df5-35a1552e2ba1@s50g2000hsb.googlegroups.com...
> On Jul 16, 4:22 am, "Doug Robbins - Word MVP"
>
>
>
>
>
> <d...@REMOVECAPSmvps.org> wrote:
> > An alternative (not necessarily superior) would be to use:
>
> > Dim mcell As Range
> > Dim i As Long
> > Set mcell =3D Selection.Cells(1).Range
> > With mcell
> > .End =3D .End - 2
> > For i =3D 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
>
> > "armsy" <sjdold...@gmail.com> wrote in message
>
> >news:bd4e3469-4507-4960-b694-16d1c6b6c53d@k30g2000hse.googlegroups.com..=
.
>
> > > 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 =3D 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 =3D
> > > 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$- Hide quoted text -
>
> > - 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?- 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"=07"Text"=07"Text" if you hover over it

but when i iterate through the sentences collection as suggested

=2ESentence[1] =3D "Text"=07
=2ESentence[2] =3D "Text"=07
=2ESentence[3] =3D "Text"=07=07=07=07

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.





Re: using for each to select sentences in a cell by Doug

Doug
Wed Jul 16 12:56:31 PDT 2008

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

"armsy" <sjdolding@gmail.com> wrote in message
news:5abebb59-cc86-4537-8bde-f467728b3374@x35g2000hsb.googlegroups.com...
On Jul 16, 9:35 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> 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
>
> "armsy" <sjdold...@gmail.com> wrote in message
>
> news:5f36aa90-f058-49d6-8df5-35a1552e2ba1@s50g2000hsb.googlegroups.com...
> On Jul 16, 4:22 am, "Doug Robbins - Word MVP"
>
>
>
>
>
> <d...@REMOVECAPSmvps.org> wrote:
> > 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
>
> > "armsy" <sjdold...@gmail.com> wrote in message
>
> >news:bd4e3469-4507-4960-b694-16d1c6b6c53d@k30g2000hse.googlegroups.com...
>
> > > 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$- Hide quoted text -
>
> > - 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?- 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.






Re: using for each to select sentences in a cell by armsy

armsy
Thu Jul 17 00:45:36 PDT 2008


> - 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

Re: using for each to select sentences in a cell by Doug

Doug
Thu Jul 17 02:34:57 PDT 2008

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

"armsy" <sjdolding@gmail.com> wrote in message
news:ecc1fe91-a6de-46ef-932d-8cbd676a33bd@p25g2000hsf.googlegroups.com...
>
>> - 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