odd behavior from .Find in 2003

C

Chip Orange

Hi all,

I'm using the .find method of a range object to locate highlighted text.

I have a document with highlighted text, which does not open showing that
it's damaged in any way, yet the .find acts differently with this document,
and I'd like to know if there's an unusual circumstance here I can program
around, or if this is undetected corruption.

Normally the range object is reset to point to the found text. In this
case, the range object is not being reset, but the .find.execute method is
returning TRUE.

Without the range object being reset I cannot deal with what I found.

Here's the relevant portion of the program below in case I'm doing something
in a way that only sometimes works:

With objCurPortionRange.Find
..ClearFormatting
..Format = True
..Highlight = True
..Text = ""
..Wrap = wdFindStop
End With

I = objCurPortionRange.Start
' loop through all blocks of highlighted text in this range
Do While objCurPortionRange.Find.Execute()
' found some highlighted text.
objCurPortionRange.Collapse wdCollapseStart
If objCurPortionRange.Characters.First.HighlightColorIndex = wdNoHighlight
Then
MsgBox "find failed to locate highlighted text, but returned true"
objCurPortionRange.Select
Exit Function
End If




thanks for any help.

Chip
 
J

Jean-Guy Marcil

Chip Orange said:
Hi all,

I'm using the .find method of a range object to locate highlighted text.

I have a document with highlighted text, which does not open showing that
it's damaged in any way, yet the .find acts differently with this document,
and I'd like to know if there's an unusual circumstance here I can program
around, or if this is undetected corruption.

Normally the range object is reset to point to the found text. In this
case, the range object is not being reset, but the .find.execute method is
returning TRUE.

Without the range object being reset I cannot deal with what I found.

Here's the relevant portion of the program below in case I'm doing something
in a way that only sometimes works:

With objCurPortionRange.Find
..ClearFormatting
..Format = True
..Highlight = True
..Text = ""
..Wrap = wdFindStop
End With

Can you post the actual code you are using to set the range and to execute
the Find?
 
C

Chip Orange

Thank you for your help. Here's a larger portion of the code:


' loop through each story type in the document
For Each objCurStoryRange In Doc.StoryRanges
x = ""
Select Case objCurStoryRange.StoryType
Case wdCommentsStory
x = "wdCommentsStory"
Case wdEndnotesStory
x = "wdEndnotesStory"
Case wdEvenPagesFooterStory
x = "wdEvenPagesFooterStory"
Case wdEvenPagesHeaderStory
x = "wdEvenPagesHeaderStory"
Case wdFirstPageFooterStory
x = "wdFirstPageFooterStory"
Case wdFirstPageHeaderStory
x = "wdFirstPageHeaderStory"
Case wdFootnotesStory
x = "wdFootnotesStory"
Case wdMainTextStory
x = "wdMainTextStory"
Case wdPrimaryFooterStory
x = "wdPrimaryFooterStory"
Case wdPrimaryHeaderStory
x = "wdPrimaryHeaderStory"
Case wdTextFrameStory
x = "wdTextFrameStory"
Case Else
x = "unknown story"
End Select
MsgBox "examining " & x

Set objCurPortionRange = objCurStoryRange

' loop through all ranges of this story
Do While Not (objCurPortionRange Is Nothing)
MsgBox "examining a range portion of " & x
With objCurPortionRange.Find
..ClearFormatting
..Format = True
..Highlight = True
..Text = ""
..Wrap = wdFindStop
End With
' loop through all blocks of highlighted text in this range
Do While objCurPortionRange.Find.Execute()
' found some highlighted text.
objCurPortionRange.Collapse wdCollapseStart
If objCurPortionRange.Characters.First.HighlightColorIndex = wdNoHighlight
Then
MsgBox "find failed to locate highlighted text, but returned true"
objCurPortionRange.Select
Exit Function
End If


Only in this one specific document am I generating the error that Find
returned true, but did not reposition the range to indicate any highlighted
text (but I have pulled this code from production while I solve this issue,
so I don't have a lot of experience running it).

thanks.

Chip
 
K

Klaus Linke

Hi Chip,

What happens if you replace .Wrap = wdFindStop with .Wrap = wdFindContinue?

Regards,
Klaus
 
C

Chip Orange

No change; thanks. It's the very first "find.execute" which yields true,
but leave the range object defined as the entire story.

Chip
 
J

Jean-Guy Marcil

Chip Orange said:
Thank you for your help. Here's a larger portion of the code:

I do not see how you reset the range before going back into the loop.
This might be a problem.

Also, I see you are trying to make a copy of the story range, presumably to
keep the original story range intact for further manipulation:
Set objCurPortionRange = objCurStoryRange
However, all this does is link the new range to the story range. So, once
the code has found some highlighted text, the story range object
(objCurStoryRange) is redefined by the Find operation to match the range of
the found text (objCurPortionRange).

If you want to keep the original story range intact, use this instead:

Set objCurPortionRange = objCurStoryRange.Duplicate

Finally, if your code works in all documents but one, you really have to
examine that document to find what makes it special.

Many different types of sections/stories?
A type of story not present in all other documents you tested?
Highlighting applied to peculiar objects/texts or combinations thereof?
Highlighting applied to a range that spans more than one sstory?
Etc.
 
C

Chip Orange

Jean-Guy Marcil said:
I do not see how you reset the range before going back into the loop.
This might be a problem.

Also, I see you are trying to make a copy of the story range, presumably
to
keep the original story range intact for further manipulation:
Set objCurPortionRange = objCurStoryRange
However, all this does is link the new range to the story range. So, once
the code has found some highlighted text, the story range object
(objCurStoryRange) is redefined by the Find operation to match the range
of
the found text (objCurPortionRange).

If you want to keep the original story range intact, use this instead:

Set objCurPortionRange = objCurStoryRange.Duplicate

Finally, if your code works in all documents but one, you really have to
examine that document to find what makes it special.

Many different types of sections/stories?
A type of story not present in all other documents you tested?
Highlighting applied to peculiar objects/texts or combinations thereof?
Highlighting applied to a range that spans more than one sstory?
Etc.

thank you for the tip on .duplicate, I'll go back to this project and try
that.

Chip
 

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