|
|
|
date: Wed, 3 Sep 2008 16:22:01 -0700,
group: microsoft.public.word.vba.userforms
back
Re: VBA Find second occurrence in table cell
On Wed, 3 Sep 2008 16:22:01 -0700, GrahamSkan
wrote:
>I am trying to find all occurrences of text in a table cell using the
>Range.Find object. If one occurrence is found, I restrict the Find range so
>that it starts at the end of that occurrence. However the Find still seems to
>start at the beginning of the cell and simply refinds the first occurrence.
>
>I am using Word 2003, but it also happens in Word 2000.
If the end of the range includes the cell marker, then the range includes the
whole cell regardless of where you set the start value. I don't know exactly
what method you're using to reset the Find range after an occurrence is found,
but a simple Range.Collapse wdCollapseEnd works.
Another problem is that, even if the initial range is only one cell, the Find
will continue through the rest of the document. To limit it to the original
range, you need to use the InRange method to test each found range. I think you
can get the principle from this simple example:
Dim myCell As Cell
Dim myRg As Range
Dim myRgOrig As Range
Set myCell = ActiveDocument.Tables(1).Cell(1, 1)
Set myRg = myCell.Range
Set myRgOrig = myRg.Duplicate
With myRg.Find
.Text = "FindMe"
While .Execute And myRg.InRange(myRgOrig)
myRg.Font.Color = wdColorRed
myRg.Collapse wdCollapseEnd
Wend
End With
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
date: Wed, 03 Sep 2008 21:46:23 -0400
author: Jay Freedman
Re: VBA Find second occurrence in table cell
"Jay Freedman" wrote:
> On Wed, 3 Sep 2008 16:22:01 -0700, GrahamSkan
> wrote:
>
> >I am trying to find all occurrences of text in a table cell using the
> >Range.Find object. If one occurrence is found, I restrict the Find range so
> >that it starts at the end of that occurrence. However the Find still seems to
> >start at the beginning of the cell and simply refinds the first occurrence.
> >
> >I am using Word 2003, but it also happens in Word 2000.
>
> If the end of the range includes the cell marker, then the range includes the
> whole cell regardless of where you set the start value. I don't know exactly
> what method you're using to reset the Find range after an occurrence is found,
> but a simple Range.Collapse wdCollapseEnd works.
>
> Another problem is that, even if the initial range is only one cell, the Find
> will continue through the rest of the document. To limit it to the original
> range, you need to use the InRange method to test each found range. I think you
> can get the principle from this simple example:
>
> Dim myCell As Cell
> Dim myRg As Range
> Dim myRgOrig As Range
>
> Set myCell = ActiveDocument.Tables(1).Cell(1, 1)
>
> Set myRg = myCell.Range
> Set myRgOrig = myRg.Duplicate
>
> With myRg.Find
> .Text = "FindMe"
> While .Execute And myRg.InRange(myRgOrig)
> myRg.Font.Color = wdColorRed
> myRg.Collapse wdCollapseEnd
> Wend
> End With
>
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>
Thank you Jay.
That bit of information did it.
The code now simply pulls the end of the range back by one character if the
last one is ASCII 7
If Asc(Right$(rng.Text, 1)) = 7 Then
rng.MoveEnd wdCharacter, -1
End If
date: Thu, 4 Sep 2008 02:45:02 -0700
author: GrahamSkan
|
|