Clear Formatting in Find

G

George Seese

When doing a With Find, ".ClearFormatting" is normally included.
I'm trying to understand the logic.

Can you provide examples of what formatting is being cleared?
Is there a situation where it should not be included?

VBA Help says about the ClearFormatting method:
"Removes any formatting specified as part of a find or replace operation.
Corresponds to the No Formatting button in the Find and Replace dialog box
(Edit menu). Note To ensure that unwanted formats aren't included as
criteria in a find or replace operation, use this method before carrying out
the operation."

1. What formatting was "specified as part of a find or replace operation"?
by whom? when?
2. When I do a manual Find in Word, (ie. find word "true"), the No
Formatting option is dimmed. The Find seems to work OK.
3. What are the "unwanted formats" that are exluded when Clear Formatting is
included?

Thanks,
George
 
J

Jay Freedman

Hi, George,

When you're doing a manual find, you can (with the cursor in the Find What
box) click the Format button and select any of the kinds of formatting
(Font, Paragraph, etc.) on the popup menu. Most of them bring up a dialog,
where you can select any of dozens of formatting items. When you return to
the Find dialog and click Find Next, the formatting you chose will be
included in the criteria that Word searches for.

These settings are "sticky" -- if you close the Find dialog, do some more
editing, and reopen the dialog, the same formatting selections will still be
set. The No Formatting button clears each of those settings back to "ignore
this in the Find criteria".

The VBA version is simply a reflection of the way that dialog works. If the
user does a manual find that included formatting criteria, and then runs
your macro that uses the Find object, the "sticky" settings will be
included. That could cause your macro not to find text that it should. You
as a macro author generally want a known base of "all clear" and then set
your own criteria. That's why you would start with .Find.ClearFormatting,
and probably also .Find.Replacement.ClearFormatting.

As always, there's a "but": The settings from the manual interface are
sticky only for Selection.Find. If you declare and use a Range object (say,
MyRange), and then search with MyRange.Find, it automatically starts with
clear formatting -- nothing from the manual interface is sticky for a Range.
(But I still throw in a .ClearFormatting, partly from habit, partly from
paranoia.) If you're using wildcard searches, there's an obscure bug that
you may need to work around, so go read
http://www.mvps.org/word/FAQs/MacrosVBA/FlushFR.htm.
 
G

George Seese

Thanks Jay,
I went back to manual Find and found it works exactly the way you explained
so clearly.
Formatting criteria from a Manual Find are carried into the next VBA Find.
The ClearFormatting method removes those criteria.

I understand the reasoning for having "sticky" format options in Manual
Finds to assist the user making repetitive finds.
But I don't see why Manual and VBA Finds should be affecting each other.

After doing several tests, I found these happening in my Word2000:
Formatting criteria from a VBA Find are not carried into the next VBA Find.
Formatting criteria from a VBA Find are not carried into a Manual Find.
Search Option criteria (ie. match whole word, case) are carried from a VBA
Find to a Manual Find.
(If an application needs the user to be doing manual finds at the same time
as running VBA doing other Finds,
it seems that the VBA could be interfering with the user.)

Thanks again for the info.
George
 
J

Jay Freedman

Hi, George,

You're correct -- if a macro is supposed to use Selection.Find interleaved
with the user's manual searches, then the macro should save all of the
user's search criteria into variables before setting up the Find object, and
restore them afterward. Since there are so many criteria, this would be a
pain to program.

That's why I recommend sidestepping the whole issue by using MyRange.Find,
which doesn't interact with manual searching at all. It has the bonus effect
that, because it doesn't move the visible selection the way Selection.Find
does, it avoids screen redraws and is usually faster.
 

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