inserting text before and after italics

B

Batailleye

Hello,

I'm trying to write a macro that finds all italics in a document and
then inserts text before and after the italics. The problem I have is
that my macro (see below) does not find all the italicized text in a
document.

Sub FindItalics()
Dim myParagraph As paragraph
For Each myParagraph In ActiveDocument.Paragraphs
With myParagraph.Range.Find
.ClearFormatting
.Font.Italic = True
.Execute
If .Found = True Then
.Parent.InsertBefore ("<em>")
.Parent.InsertAfter ("</em>")
End If
End With
Next
End Sub
 
G

Graham Mayor

Try

Sub ReplaceExample()
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
'**********************
.Text = ""
.Font.Italic = True
.Replacement.Text = "< em >^&</em>"

'**********************
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute replace:=wdReplaceAll
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi Batailleye,

just another one, in case you want to use range.
Of course, you may use replacement.text as well.

Sub Test333()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Font.Italic = True
While .Execute
rTmp.Font.Italic = False
rTmp.InsertBefore "<em>"
rTmp.InsertAfter "</em>"
rTmp.End = ActiveDocument.Range.End
Wend
End With
End Sub

Note: Remove the formatting you are looking for!
Otherwise you may end up with multiple tags
like </em></em></em> when repeating the macro.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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