如何使用 VBA 将样式应用于 Word 中的多个选择?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1284867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:41:42  来源:igfitidea点击:

How do I apply a style to multiple selections in Word using VBA?

vbams-wordcoding-styleselection

提问by JohnZaj

I created a macro that will apply a particular style to whatever is selected in the document. However, when in draft view, when the user clicks in the style area pane to select a paragraph and then Ctrl + clicks on an additional paragraph, this additional selection is not applied when this macro is run:

我创建了一个宏,它将特定样式应用于文档中选择的任何内容。但是,在草稿视图中,当用户在样式区域窗格中单击以选择一个段落,然后 Ctrl + 单击一个附加段落时,运行此宏时不会应用此附加选择:

Sub BodyTextApply()
    Selection.Style = ActiveDocument.Styles("Body Text,bt")
End Sub

What do I need to add to this? Note: The workflow cannot change so that the user selects that actual text in the document; they are set on using the style area pane...

我需要添加什么?注意:工作流程不能更改,以便用户选择文档中的实际文本;它们设置为使用样式区域窗格...

The workflow is as follows:

工作流程如下:

alt text http://img6.imageshack.us/img6/1994/91231840.png

替代文字 http://img6.imageshack.us/img6/1994/91231840.png

The (undesired) output is as follows:

(不需要的)输出如下:

alt text http://img34.imageshack.us/img34/1239/outputt.png

替代文字 http://img34.imageshack.us/img34/1239/outputt.png

回答by Dirk Vollmar

Looks like your style "Body Text,bt" is a pure paragraph style. That means it will only be applied to a complete paragraph.

看起来您的样式“正文,bt”是纯段落样式。这意味着它只会应用于一个完整的段落。

However, in your screenshot there is only part of the second paragraph selected. Make sure the complete paragraph is selected or, if the style should only be applied to part of the paragraph, make your style "Body Text,bt" a linked (paragraph and character) style.

但是,在您的屏幕截图中,仅选择了第二段的一部分。确保选择了完整的段落,或者,如果样式只应应用于段落的一部分,请将您的样式“正文,bt”设为链接(段落和字符)样式。

Programmatic access to discontiguous selections is very limited, and such selections can only be created using the Word UI. The MSDN article Limited programmatic access to Word discontiguous selectionsgives some more details.

对不连续选择的编程访问非常有限,并且只能使用 Word UI 创建此类选择。MSDN 文章Limited programmatic access to Word discontiguous selections提供了更多详细信息。

If applying the style only to part of the paragraph (by making it a linked style) is not what you want you probably have to come up with a hack like this:

如果仅将样式应用于段落的一部分(通过使其成为链接样式)不是您想要的,您可能必须想出这样的技巧:

Sub BodyTextApply()

    Dim oRange As Range

    ' create a temporary character style
    ActiveDocument.Styles.Add "_TEMP_STYLE_", wdStyleTypeCharacter

    ' apply the temporary style to the discontiguous selection
    Selection.Style = ActiveDocument.Styles("_TEMP_STYLE_")

    Set oRange = ActiveDocument.Range

    With oRange.Find
        .ClearAllFuzzyOptions
        .ClearFormatting
        .ClearHitHighlight
        .Style = ActiveDocument.Styles("_TEMP_STYLE_")
        .Text = ""
        .Wrap = wdFindStop

        ' search for all occurences of the temporary style and format the
        ' complete paraphraph with the desired style
        Do While .Execute
            oRange.Paragraphs(1).Style = ActiveDocument.Styles("Body Text,bt")
        Loop

    End With

    ' cleanup
    ActiveDocument.Styles("_TEMP_STYLE_").Delete

End Sub

You probably want to add some error handling as well to make sure that the temporary style used is finally removed from the document.

您可能还想添加一些错误处理,以确保最终从文档中删除所使用的临时样式。