Dreamtale
Dreamtale

Reputation: 13

Replace italic text with tags

Okay, so I am trying to replace all italic text in my document to "%text%":

Sub Italic()
Set oFound = ActiveDocument.Content
    With oFound.Find
        .ClearFormatting
        .Text = ""
        .Font.Italic = True
        .Replacement.Text = "<i>" + %something here% + "</i>"
        .Execute Replace:=wdReplaceAll
    End With
End Sub

I'm guessing I need to find out how to refer to the found text: %something here%? Or maybe there is another way?

Upvotes: 1

Views: 390

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

To find all italic text and replace it with itself, plus text before and after use the special character ^& as part of the replacement text string. For example:

Sub TestReplaceInItalic()
    Dim replaceText As String

    replaceText = "%^&%"
    With ActiveDocument.content.Find
        .ClearFormatting
        .Format = True
        .Font.Italic = True
        .Text = ""
        .Replacement.Text = replaceText
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Upvotes: 1

Related Questions