VBA MS-Word Work with mutiple discontinuous selection?

两盒软妹~` 提交于 2019-12-31 02:46:06

问题


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?

Example:

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

I'm working like this:

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

回答1:


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.

The new Java Script APIs can do this BTW.

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.



来源:https://stackoverflow.com/questions/33730750/vba-ms-word-work-with-mutiple-discontinuous-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!