Word 2003 - 如何使用宏来更改样式?

时间:2010-03-10 23:37:01

标签: ms-word word-vba

是否可以创建Word 2003宏来更改文档某些段的字体样式?

例如,假设我有一个文档,其中大部分文本为粗体斜体和12磅字体。我想用带有下划线的14点字体替换所有具有这些特征的文本。

我已经在Google,StackOverflow和Microsoft's website上进行了一些搜索,但我无法找到任何可以讨论的内容。

有任何帮助吗?

2 个答案:

答案 0 :(得分:4)

是的,您需要使用.Find对象及其子.Replacement内容。您可以在Selection(限制版),Range(段落,故事等)或整个文档中执行此操作。以下示例适用于整个文档(ActiveDocument.Content)。

Sub FindReplaceStyle()
    With ActiveDocument.Content.Find
        .ClearFormatting

        With .Font
            .Bold = True
            .Size = 14
            .Italic = True
        End With

        .Format = True

        With .Replacement
            .ClearFormatting
            With .Font
                .Bold = False
                .Italic = False
                .Underline = wdUnderlineSingle
                .Size = 12
            End With
        End With

        .Execute Forward:=True, Replace:=wdReplaceAll, _
            FindText:="", ReplaceWith:=""
    End With
End Sub

答案 1 :(得分:2)

与往常一样,Word的宏录制器可以提供很大的帮助:

Selection.Find.ClearFormatting
With Selection.Find.Font
    .Size = 12
    .Bold = True
    .Italic = True
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
    .Size = 14
    .Bold = False
    .Italic = False
    .Underline = wdUnderlineSingle
End With
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 Replace:=wdReplaceAll

(要生成宏启动宏录制器,请按Ctrl + H打开“查找和替换”,然后指定相应的查找格式和替换格式)