如何使用VBA将样式应用于Word中的多个选择? [英] How do I apply a style to multiple selections in Word using VBA?

查看:827
本文介绍了如何使用VBA将样式应用于Word中的多个选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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



工作流程如下:





(不需要的)输出为



解决方案

看起来像你的风格Body Text,bt是一个纯粹的段落风格。这意味着它只会应用到一个完整的段落。



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



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



如果仅将样式应用于段落的一部分(通过使其成为链接样式)不是您想要的,您可能需要提交一个黑客这:

  Sub BodyTextApply()

Dim oRange As Range

'创建临时字符样式
ActiveDocument.Styles.Add_TEMP_STYLE_,wdStyleTypeCharacter

'将临时样式应用于不连续的选择
Selection.Style = ActiveDocument.Styles( _TEMP_STYLE_)

设置oRange = ActiveDocument.Range

使用oRange.Find
.ClearAllFuzzyOptions
.ClearFormatting
.ClearHitHighlight
.Style = ActiveDocument.Styles(_ TEMP_STYLE_)
.Text =
.Wrap = wdFindStop

'搜索临时样式的所有出现,并格式化
'完成paraphraph与所需的风格
Do While .Execute
oRange.Paragraphs(1).Style = ActiveDocument.Styles(Body Text,bt)
Loop

结束于

'清除
ActiveDocument.Styles(_ TEMP_STYLE _)。删除

结束子

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


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:

The (undesired) output is as follows:

解决方案

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.

这篇关于如何使用VBA将样式应用于Word中的多个选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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