The ClearFormatting, clears any prior formatting
settings, whereas the Format= False, doesn't include
formatting in the search.
Think of ".Format = " as the switch that enables or
disables the search for a format. ClearFormatting is used
to wipe the slate clean in effect when doing a format
based search or replace.
Having .Format = True and then ClearFormatting, would
search for a "Clear" format. By stating .Format = False,
you simply won't search for the format in the first place.
Not entirely. Maybe if I can test this I'll understand it better. But
how to test it. The equivalent in the Find dialog box of
ClearFormatting is the No Formatting button, but what would be .Format =
be the equivalent of in the Find box?
I tested it like this. In the first macro, I'm looking for underlined
text, and I have .Format = True. The macro works and finds the
underlined word "happy." In the second macro, I make .Format = False
and leave everything else the same. The macro still works and finds the
underlined word. So how is .Format operating as a switch here?
Selection.Find.ClearFormatting
Selection.Find.Font.Underline = wdUnderlineSingle
With Selection.Find
.Text = "happy"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute
----------------------
Selection.Find.ClearFormatting
Selection.Find.Font.Underline = wdUnderlineSingle
With Selection.Find
.Text = "happy"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
End With
Selection.Find.Execute
Ok, I did that, and see what you mean. So the .Format = is a switch
that turns off any existing formatting commands in the Find command. I
don't know what the purpose of that would be, except to make it easier
to change code. Instead of deleting all the lines in a Search code
related to formatting, you just stick in the .Format = False and that
turns them off. But in an ordinary macro, say a Find macro created by
recording, is there any need to keep the ".Format =" ?
On a simple find, it really doesn't matter so much which
method you use (either turn off formatting, or clear it).
The ClearFormatting comes in handy when doing
find/replace actions. For example you may search for a
format and then replace it with clear formatting
(i.e. .Replacement.ClearFormatting).
When using the macro recorder you'll find that it
automatically writes down all the available options. You
can generally strip a heap of stuff from recorded macros
without impact.
The recorder is a great way to learn about VBA
functionality, but when you start writing code by hand
you'll start to find much quicker and simpler methods a
lot of the time.
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.