Selection.Find.Found returns true after all items have been found

S

Steven Lee

Hi,

We recently migrated from Word 2000 to Word 2003. We have a ton of VBA code
that we use in house and are finding some issues between the two versions.

We have code that looks through a document and finds italic, bold, etc. and
wraps the text with codes similar to HTML (we're coding for InDesign and
Ventura)

When running a coding macro to tag italic formatting on a document that has
tables I found that Selection.Find.Found returns true, and the macro
continues to run, even though we've found and tagged all the italic.

'These are the tags we're adding for italic
startcode$ = "<I>"
endocde$ = "<I*>"

'First I set up the search:
With Selection.Find
.ClearFormatting
.Font.Italic = True
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

'Then I execute the search

Selection.Find.execute

While Selection.Find.Found
ActiveDocument.UndoClear

Selection.MoveLeft wdCharacter, 1 'start of the italic
string
Selection.TypeText startcode$ 'add the startcode
If Len(startcode$) <> 0 Then
'go back over the startcode and set it to default
para font
Selection.MoveLeft wdCharacter, Len(startcode$),
wdExtend
Selection.Style = "Default Paragraph Font"

Selection.MoveRight wdCharacter, 1
'we're back at the start of the italic string

Selection.Find.Execute
'puts the cursor at the end of the string
Selection.MoveRight wdCharacter, 1
'we're at the end of the italic string
Selection.TypeText Text:=endcode$ 'add the
end code

If Len(endcode$) <> 0 Then
'swipe the end code and set it to default para
Selection.MoveLeft wdCharacter, Len(endcode$),
wdExtend
Selection.Style = "Default Paragraph Font"
Selection.MoveRight wdCharacter, 1
'we're at the end of the endcode, ready to look for
more italic
End If

Selection.Find.Execute
'when we hit the last of the italic Selection.Find in
Word 2000, selection.find.found = false and we exit the loop, but in 2003
it's true, but when the search executes again the cursor stays put and keeps
on typing startcode$ and endcode$ over and over as infinitum.
Wend

I can't turn off the italic (what if it's bold and italic, then I need to
come back and do bold.)

If I run this on a document with no tables it works fine.

Any help would be most appreciated!!

Thanks in advance,
Steven Lee

Vaporloop - Technology Solutions for Small Businesses
 
K

Klaus Linke

Hi Steve,

I think you made a pretty simple task pretty complicated.

Why not just search for all "italic", and replace with

.Replacement.Text = startcode$ & "^&" & endcode$
' ...
.Wrap=wdFindStop,
' ...
End With
Selection.Find.Execute Replace:=wdReplaceAll

Some other comments on your code:
I can't turn off the italic (what if it's bold and italic, then I
need to come back and do bold.)

I don't see why you can't. If you replace with .Italic=False, bold text
stays bold and you can tag it later.
If I run this on a document with no tables it works fine.

Maybe the way you select stuff selects more than you want... In tables, it's
easy to accidentally select the end-of-cell- or end-of-row-markers.
It should not be too hard to make a small problematic sample, and
single-step through the macro (F8), watching what gets selected in the
document.
Selection.MoveLeft wdCharacter, 1
...
Selection.MoveRight wdCharacter, 1

If you want to collapse the Selection, better use
Selection.Collapse(wdCollapseStart)
Selection.Collapse(wdCollapseEnd)

Regards,
Klaus
 
R

Rob

In Word 2003 this works fine for me, tables or not, and is a lot less work.

With Selection.Find
.Font.Italic = True
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute

While Selection.Find.Found = True
Selection.Font.Italic = False
Selection.Text = "<I>" & Selection.Text & "<I*>"
Selection.MoveRight wdCharacter, 1
Selection.Find.Execute
Wend
 
R

Rob

In Word 2003 this works fine for me, tables or not, and is a lot less code.

With Selection.Find
.Font.Italic = True
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute

While Selection.Find.Found = True
Selection.Font.Italic = False
Selection.Text = "<I>" & Selection.Text & "<I*>"
Selection.MoveRight wdCharacter, 1
Selection.Find.Execute
Wend
 
R

Rob

No, I don't just like hearing myself talk (seeing myself write?). The double
post was due to a forced refresh after a timeout and I can't delete it.
 
S

Steven Lee

Klaus and Rob,

Thanks so much for your suggestions. I tried both and the problem
continues.

The issue is less about the way I'm tagging the text, and more about the
fact that selection.find.found is still returning true after I've passed over
the document. I've posted the file on my site if you are inclined to see if
the problem occurs for you.

www.vaporloop.com/stuff/problem_file.doc

When my code gets to the phrase "2nd Trial" it should be done finding any
italic.

Selection.Find.Found should be false
Selection.Find.Execute does NOT find any more italic (ie it doesn't go back
to the italic text at the start of the file.)

Thanks again for your help.

Steve
 
R

Rob

I downloaded your file and tried the code I posted and it worked fine. It
found 5 instances of italic, did the replace, and stopped. We are both using
Word 2003 so this is really strange.

Regardless, I tried to find a way to do it without using .Found, and came up
with this, which also works flawlessly for me.

With Selection.Find
.Font.Italic = True
.Forward = True
.Wrap = wdFindStop
Do While .Execute
Selection.Font.Italic = False
Selection.Text = "<I>" & Selection.Text & "<I*>"
Selection.MoveRight wdCharacter, 1
Loop
End With
 
E

Ed

I downloaded your file and tried the code I posted and it worked fine. It
found 5 instances of italic, did the replace, and stopped. We are both using
Word 2003 so this is really strange.

Regardless, I tried to find a way to do it without using .Found, and came up
with this, which also works flawlessly for me.

With Selection.Find
.Font.Italic = True
.Forward = True
.Wrap = wdFindStop
Do While .Execute
Selection.Font.Italic = False
Selection.Text = "<I>" & Selection.Text & "<I*>"
Selection.MoveRight wdCharacter, 1
Loop
End With

Steven -

One thing I have had to do when some bug I'm not good enough to catch
gets me like this is restrict my search range. Set a range to
YourDoc.Content. Then set the range you will search to the doc
range.Duplicate. Your .Find will reset the search range to encompass
only the found text. When you're done working with it, collapse the
search range to the end, then reset the serach range to Search
Range.Start, Doc Range.End. Thus your search range keeps shrinking
until it's just the end of the document.

Ed
 

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