如何使用宏自动格式化Word中的文本?

时间:2017-07-01 15:49:53

标签: ms-word automation ms-office word-vba office-automation

让我们说我有一个word文档,其中我有一些文本样式,如Heading1,Heading2等。现在我有每种格式的特定格式样式,我想使用宏来应用这些样式。而且我想检查是否使用宏来使用正确的样式。有没有办法可以做到这些?

1 个答案:

答案 0 :(得分:0)

好的,根据您的问题,我希望自动替换样式。

这是一个可以帮助你做到这一点的宏:

Public Sub SearchReplaceStyles()

Dim search_style As String ' the style which apparently seem out of style
Dim replace_style As String ' the desired style

search_style = "Heading 1"
replace_style = "Heading 2"

With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles(search_style)
    .Replacement.ClearFormatting
    .Replacement.Style = ActiveDocument.Styles(replace_style)
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
End With

End Sub