VBA MS-Word是否可以使用多个不连续选择? [英] VBA MS-Word Work with mutiple discontinuous selection?

查看:472
本文介绍了VBA MS-Word是否可以使用多个不连续选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做多项选择.我想在每个选定的段落之前添加一些文本,但是当我选择多个不连续的段落时,如果执行Selection.Paragraphs.Count,我总是得到"1".

I'm trying to do something with a multiple selection. I wanna add some text before every selected paragraph but, when I select multiple discontinuous paragraphs, if I do Selection.Paragraphs.Count I always get "1".

我如何分开处理所有段落?

How could I work with all paragraphs apart?

示例:

Paragraph1(Selected first)
Paragraph2
Paragraph3(Selected second)

当我尝试在这些段落的开头添加一些文本时,我得到了什么:

What I got when I try to add some text at the beginning of these paragraphs:

Paragraph1
Paragraph2
TEXTParagraph3

我真正想要获得的东西:

What I really want to obtain:

TEXTParagraph1
Paragraph2
TEXTParagraph3

我正在这样工作:

sub x()
  dim p as paragraph
  for each p in selection.paragraphs
    p.range.insertbefore("TEXT")
  next
End sub

推荐答案

Word根本无法满足您的要求.自2003年引入多项选择以来,开发人员就一直希望这样做(我想可能是2007版). Word的对象模型根本不支持它.

Word simply cannot do what you'd like for it to do. Developers have wished for this since multiple selections were introduced in 2003 (I think it was, might have been version 2007). Word's object model simply does not support it.

如果要提供给用户使生活更轻松,则需要为该工具提供一种标记段落的方法,以便您的代码可以识别它们.例如,您可以提供一个宏,该宏为每个选择分配一个递增的书签名称(用户选择,然后运行您的宏;对每个段落重复一次).然后,您的代码可以处理每个书签并执行操作.为了使它更加用户友好,您可以将宏分配给键盘快捷方式和/或功能区/QAT和/或右键单击菜单中的按钮.

If this is something you want to provide to the user to make life easier you'll need to give the tool a way to mark the paragraphs so your code can recognize them. You could provide a macro, for example, that assigns an incrementing bookmark name to each selection (the user selects, then runs your macro; repeat for each paragraph). Your code can then address each bookmark and perform the actions. To make this more user friendly you can assign the macro to a keyboard shortcut and/or a button in the Ribbon/QAT and/or the right-click menu.

这篇关于VBA MS-Word是否可以使用多个不连续选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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