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

发布于 2024-08-01 23:26:15 字数 635 浏览 3 评论 0原文

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

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

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

工作流程如下:

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

(不需要的)输出如下:

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

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:

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

The (undesired) output is as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-08-08 23:26:15

看起来您的样式“Body Text,bt”是纯段落样式。 这意味着它仅适用于完整的段落。

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

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

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

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

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

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

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.

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 selections gives some more details.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文