使用多个不连续选择 [英] Working with multiple discontinuous selection

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

问题描述

我正在尝试使用多项选择来做某事.我想在每个选定的段落之前添加一些文本,但是,当我选择多个不连续的段落时,如果我执行 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

我真正想得到的:

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.

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

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