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.