Discussion:
VBA code to make a macro that finds italics
(too old to reply)
LizW
20 years ago
Permalink
Hi there,

We didn't have any luck with the Tools, Macro Record feature when trying to
find italics with the find dialog box. The macro won't run.

Is there a way to do it in VBA?

Thanks
Jay Freedman
20 years ago
Permalink
Post by LizW
Hi there,
We didn't have any luck with the Tools, Macro Record feature when
trying to find italics with the find dialog box. The macro won't run.
Is there a way to do it in VBA?
Hi Liz,

The braindead recorder is sometimes completely worthless. In this case, it
records the fact that you're looking for formatting, but it forgets to
record what kind of formatting you asked for!

Press Alt+F8 to open the list of macros, select the name you gave to the
recorded macro, and click the Edit button. In the editor, you'll see
something like this:

Sub FindItalic()
'
' FindItalic Macro
' Macro recorded 11/23/2004 by ...
'
Selection.Find.ClearFormatting
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

After the line .Text = "" you should insert the missing line:

.Font.Italic = True
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
LizW
20 years ago
Permalink
This is how the macro reads now, but the cursor just stays still. What have I
done wrong? Thanks!!!


Sub macro3()
'
' macro3 Macro
' Macro recorded 11/23/2004 by lweiman
'
Selection.find.ClearFormatting
With Selection.find
.Text = .Font.italic = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub
...
LizW
20 years ago
Permalink
NEVER MIND, PUT IT AFTER LIKE YOU SAID AND IT WORKED!

AWESOME! THANKS!!
...
LizW
20 years ago
Permalink
Spoke too soon.

It finds one italicized item but when I try to get it to find the next one,
it stays in place. I have to manually move it after to get it to find the
next, with varying results. We need to search for italics throughout the
document and I was hoping to hit Alt I to find the first, mark it, and then
hit Alt I again to find the next instance.

Is there extra code I have to put in for that?

Thanks again.
...
Jay Freedman
20 years ago
Permalink
Yes, you need more code to do anything more than just going to the text.

The version you showed in one of your posts has the line
Selection.MoveLeft Unit:=wdCharacter, Count:=1
after the .Execute. That moves the cursor to the left of the italic text. If
you leave the cursor there, the next Find is going to find the same piece
again -- you have to start the next search *after* the end of the first
italic text.

This might be easier to do with a Replace instead of a Find, then you can
just do one Replace All and be done. If you tell me exactly how you "mark"
the text, I can probably come up with a do-it-all macro.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
...
Loading...