如何在VBA中的范围/选择中替换Microsoft Word字符样式? [英] How can I replace a Microsoft Word character style within a range/selection in VBA?

查看:212
本文介绍了如何在VBA中的范围/选择中替换Microsoft Word字符样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有宏的Word 2007模板,该宏会将字符样式应用于所选文本.似乎查找/替换"功能将是一个不错的起点,但是我认为我发现了一个错误/限制,该错误/限制使宏无法按预期工作.

I'm working on a Word 2007 template with a macro that will apply character styles to the selected text. It seemed that the Find/Replace feature would be a good place to start, but I think I've found a bug/limitation that prevents the macro from working as desired.

这是我的vba代码:

Sub restyleSelection()
    Dim r As Range
    Set r = Selection.Range
    With r.Find
        .Style = ActiveDocument.Styles("Default Paragraph Font")
        .Text = ""
        .Replacement.Text = ""
        .Replacement.Style = ActiveDocument.Styles("Emphasis")
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub

如果我创建一个包含几个段落的测试文档,并在其中一个段落中选择了几个单词,然后运行宏,则"Emphasis"样式将不仅应用于所选内容,还将应用于所选内容的结尾到文档末尾.

If I create a test document that contains a few paragraphs and select a few words in one of the paragraphs, then run the macro, the "Emphasis" style is applied not only to the selection, but beyond the end of the selection to the end of the document.

使用实际的GUI查找/替换工具,此行为是相同的.

This behavior is the same using the actual GUI Find/Replace tool.

我的问题是:我该如何克服此错误/限制并在选择/范围内应用文字样式?

My question is: How can I overcome this bug/limitation and apply the character style ONLY within the selection/range?

更多信息:
我真正需要宏执行的操作是在整个选择中应用某些格式,同时保持选择中的现有字符样式.例如,如果所选文本包含粗体,斜体,其余为默认段落字体,则宏应将粗体替换为"Revised Bold",将"Italic"替换为"Revised Italic",并且将默认段落字体"替换为修订".这样,当我使用伴随宏撤消"该宏的操作时,可以替换原始字符样式(粗体,斜体,默认段落字体).

A little more information:
What I really need the macro to do is apply certain formatting to the entire selection while maintaining the existing character styles in the selection. For example, if the selected text contains the Bold character style, the Italic character style, and the rest of it is Default Paragraph Font, the macro should replace Bold with "Revised Bold", replace "Italic" with "Revised Italic", and replace "Default Paragraph Font" with "Revised". That way, when I use the companion macro to "undo" the action of this macro, the original character styles (Bold, Italic, Default Paragraph Font) can be replaced.

已解决:
这是我终于到达的解决方案:

SOLVED:
Here is the solution I finally arrived at:

Sub applyNewRevisedText
    Dim r As Range          ' Create a new Range object
    Set r = Selection.Range ' Assign the current selection to the Range
    Dim rng As Range
    For Each rng In r.Words
        Set rngStyle = rng.Style
        Select Case rngStyle
        Case "Bold"
            rng.Style = ActiveDocument.Styles("New/Revised Text Bold")
        Case "Italic"
            rng.Style = ActiveDocument.Styles("New/Revised Text Emphasis")
        Case Else
            rng.Style = ActiveDocument.Styles("New/Revised Text")
        End Select
    Next rng
End Sub

推荐答案

回答您的直接问题

我的问题是:如何克服该错误/限制并应用 字体是否仅在选择/范围内?

My question is: How can I overcome this bug/limitation and apply the character style ONLY within the selection/range?

这不满足需要吗?:

Sub restyleSelection()
    Selection.Style = ActiveDocument.Styles("Emphasis")
End Sub

好吧,根据您的评论,类似这样的事情:

Ok, based on your comment, what about something like:

Dim rng As Range
  For Each rng In Selection.Words
    If rng.Bold 'do something
  Next rng

.单词会将范围内的每个单词分解为范围的集合.然后,您可以根据每个单词的当前样式对它们进行样式设置.

.Words will break up each word in the range into a collection of ranges. Then you can perform styling on each individual word based on its current style.

这篇关于如何在VBA中的范围/选择中替换Microsoft Word字符样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆