Fatal difference between Word app Find op and same op when recorded with a macro

I

Ian Jaffray

Hello,

To recreate this, create a new doc based on normal.dot, put in a few
characters of text and style them with, for example, Heading 2. The record a
macro of using Find to find the style Heading 2. Close the macro, then run
it on the same document in VBA.

After that, open the Find dialog in Word. On the Find tab, where it says
"Find what:", Format will read:

"Style: Heading 2, Border: Top (Single solid line, Auto, 0.75 pt Line
width)..."

Heading 2 does not have a Border Top property matching that, confirmed by
simple visual inspection and the "Reveal Formatting" properties.

If you immediately try and run the Find operation from the dialog, it fails
to find the text styled with Heading 2.

Does anyone know why the anomalous info is appearing in the Find dialog, or
why the macro of the recorded Find op is failing?

Thanks for your help,

Ian J.


The macro that gets created for the simple Find op is:

Sub Macro121()
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub
 
C

Charles Kenyon

Recorded macros don't always work. Sometimes they do, other times they are a
kludge or a start to writing macros that will work. The code they generate
is almost always too much. They also use the selection object when the range
object would be a better choice. See
http://word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm - especially the
part on broken replace macros. You may also want to look at
http://word.mvps.org/FAQs/MacrosVBA/ClearFind.htm.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
I

Ian Jaffray

Dear Charles,

Thanks for your help, I'll check out the links you provided.

Sincerely,

Ian J.
 
I

Ian Jaffray

Dear Charles,

So, it worked to rewrite the Find/Replace, putting in the properties I
wanted and taking out those that did nothing, both in that the routine now
finds the text I was looking for, replaces it with what I wanted, and does
not create odd "Format" information in the Find/Replace dialog afterwards.

I learned a little more than usual today, thanks to you!

Sincerely,

Ian Jaffray
 

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