Word中许多短语的斜体

时间:2018-04-28 23:29:10

标签: vba ms-word word-vba

我一直在研究一种代码,它可以找到并替换单词以使它们成斜体。但是,我无法弄清楚如何使用数组提高效率。 目前我的代码是我只是继续复制和粘贴with循环:

    Sub ItalicsText()
'
' ItalicsText Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Italic = True
    With Selection.Find
        .Text = "Lord of the Rings"
        .Replacement.Text = "Lord of the Rings"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Selection.EscapeKey
End Sub

但是,我想这样做,以便我可以有一个像:

这样的数组
vFindText = Array("Lord of the Rings", "blah", "blah")

我想这样做,因为我需要检查数百个短语,并希望让我更快地进行编码。

1 个答案:

答案 0 :(得分:0)

未测试:

Sub AllTexts()
    Dim vFindText, v
    vFindText = Array("Lord of the Rings", "blah", "blah")
    For Each v in vFindText 
        ItalicsText v
    Next v
End Sub


Sub ItalicsText(findWhat)

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Italic = True
    With Selection.Find
        .Text = findWhat
        .Replacement.Text = findWhat
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.EscapeKey
End Sub