Find/Replace bug?

  • Thread starter Jean-Guy Marcil
  • Start date
J

Jean-Guy Marcil

Hi there!

I have a routine that looks for highlighted text and then copies each
instance of such found text in a new document.

The main part of the main sub goes like this:

Dim rgeDoc As Range
Dim lngWordCount As Long
Dim i As Long
Dim docNew As Document
Dim docCurrent As Document

Set rgeDoc = ActiveDocument.Range
lngWordCount = 0

Set docCurrent = ActiveDocument
Set docNew = Documents.Add

'wdMainTextStory = 1 and wdTextFrameStory = 5
CheckRange rgeDoc, docNew, docCurrent

i = 1
With docCurrent.Shapes
If .Count > 0 Then
For i = 1 To .Count
If .Item(i).Type = msoTextBox Or .Item(i).Type = msoAutoShape Then
If .Item(i).TextFrame.HasText Then
Set rgeDoc = .Item(i).TextFrame.TextRange
CheckRange rgeDoc, docNew, docCurrent
End If
End If
Next
End If
End With


I have to search each shape separately because the built-in Find ignores them.
This sub calls the following function:


Function CheckRange(ByVal rgeFind As Range, _
ByVal docCopy As Document, ByRef docFrom As Document)

Dim rgeKeep As Range
Dim rgeCopy As Range

Set rgeKeep = rgeFind.Duplicate

If rgeFind.StoryType = 1 Then
With rgeFind.Find
.Highlight = True
.Format = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
Set rgeCopy = docCopy.Range
rgeCopy.Collapse wdCollapseEnd
rgeCopy.FormattedText = .FormattedText
rgeCopy.Collapse wdCollapseEnd
rgeCopy.InsertParagraphAfter
.Collapse wdCollapseEnd
If .End = docFrom.Range.End Then Exit Do
If Asc(.Characters(1)) = 13 Then
.MoveStart wdCharacter, 1
End If
Set rgeFind = docFrom.Range(.Start, docFrom.Range.End)
End With
Loop
End With
Else
With rgeFind.Find
.Highlight = True
.Format = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
Set rgeCopy = docCopy.Range
rgeCopy.Collapse wdCollapseEnd
rgeCopy.FormattedText = .FormattedText
rgeCopy.Collapse wdCollapseEnd
rgeCopy.InsertParagraphAfter
.Collapse wdCollapseEnd
If .End = rgeKeep.End Then Exit Do
If Asc(.Characters(1)) = 13 Then
.MoveStart wdCharacter, 1
End If
rgeFind.SetRange .Start, rgeKeep.End
End With
Loop
End With
End If

End Function


I needed to have two "With rgeFind.Find" because the ranges are handled
differently if they are in the main story or part of a text frame.

Here is the context:
I have a document that has Figures made up of many textboxes connected by
lines. They were created by converting embeded Visio drawings into enhanced
meta files, and then edited by right clicking on them. The Figures were
cleaned up so that they consit only of lines and textboxes containing
textframes. All useless shapes that the Edit command created have been
removed. (This complicated process was used because it would have been too
long to recreate the complex Figures from scratch within Word. It was overall
faster to convert these complex English Figures into editable ones so that
people who do not have Visio installed can easily translate them.) Those
figures are held within a 1x1 table.

Here is the bug:
All highlighted text in the main story is picked up.
Many of the shapes within the figures have highlighted text in them. Only
about 5% of the highlighted text is being picked up by the function. I
debugged the code and I know that the second ""With rgeFind.Find" above is
being executed, but the corresponding "Do While .Execute" fails, as if the
range from the textframes did not contained highlighted text when in fact it
does...

¿Qué este pasa?
 
H

Helmut Weber

Hi Jean-Guy Marcil,

if you could send me a sample doc,
I would try my best.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jean-Guy Marcil

Helmut Weber said:
Hi Jean-Guy Marcil,

if you could send me a sample doc,
I would try my best.

Thanks Helmut, I appreciate the offer, unfortunately, I am bound by a
Confidentiality Agreement...

It would take too long to remove all the text and replace it by text of the
same length... Plus I am not convinced that modifying the document in such a
way would have no impact on the problem...

I just thought that I would post my problem in case someone had seen the
same behaviour before.

Thanks again.
 
T

Tony Jollans

Hi Jean-Guy,

I don't have time to mock it up at the moment but ...

Do any of the shapes have text in them that is *all* highlighted? If so,
Find won't find it because it assumes it was the result of the previous Find
(more or less).
 
J

Jean-Guy Marcil

Tony Jollans said:
Hi Jean-Guy,

I don't have time to mock it up at the moment but ...

Do any of the shapes have text in them that is *all* highlighted? If so,
Find won't find it because it assumes it was the result of the previous Find
(more or less).

Thanks Tony, that was it.

Instead of selecting the shape border and applying the highlight, I have to
select the whole shape text content but the last paragrpah mark before
applying the highlight. Then the code executes correctly.

I do believe that this is a bug because...
1) My code goes through the Shape collection and defines a new range object
for each shape's TextFrame content. After such a range object is defined the
code executes the Find. At that point, Word should not be aware of the fact
that surounding textframes in the document have been/will be searched...
2) I have 4 flowcharts in the document, they all have been produced in the
same manner and all contain shapes whose entire text content has been
highlighted by selecting the shape border before appying the highlight.
However, the code returns nearly the full content from some flow charts, but
not others... The behaviour seems to be inconsitent.

Unless I missed something in my code! Which is entirely possible...
 
T

Tony Jollans

It's a consequence of the way Find works - I'm not sure it's a bug. Find
does not really have knowledge of what's gone before - it infers it from the
current contents of the Range. If you collapse the range (to start) before
doing the first Find it should correctly search the textframe - I think :)
 

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