none
To get all selected elements in the MS Word document RRS feed

  • Question

  • I am developing a MS Word application. How can I get all selected elements in the document?

    Thank you for help.

    Monday, June 29, 2015 11:08 PM

Answers

  • Hi Ya

    Unfortunately, the Word object model does not provide a way to do what you wish. It's something Word developers have wished for since the capability to make multiple selections was introduced, but it's not exposed to the object model.

    This is something that might become part of the "Word app" (newly designated Word add-in) APIs for coming versions of Word, but is not currently available.

    The closest you can get would be to use the COPY method: Selection.Copy. Then paste this somewhere (a new document, for example). The entire selection will be pasted, with a paragraph mark (ANSI 13) separating each selection. Perhaps you can do something with that...


    Cindy Meister, VSTO/Word MVP, my blog

    • Proposed as answer by L.HlModerator Wednesday, July 1, 2015 3:36 AM
    • Marked as answer by L.HlModerator Tuesday, July 7, 2015 5:50 AM
    Tuesday, June 30, 2015 3:39 PM
    Moderator

All replies

  • Hi,

    If selected elements are all Text, you can do like this. 

    Dim txt As String
    txt = Selection.Text
    If selected elements include various types(e.g. Text, Shape, etc), you can copy the selection.
    Selection.Copy
    I'd like to know what you want to do after getting all selected elements.


    Regards,

    Tuesday, June 30, 2015 1:31 AM
  • I've selected some discrete text elements and then got range of this selection. The range was range of the last selected element. How can I get ranges of all selected text elements?

    For example, in the string "test test test test" I've selected 1-st word and 3-rd word. I got range with the start position 10 and the end position 14.

    Tuesday, June 30, 2015 11:23 AM
  • Hi,

    I can hardly understand your need. Please provide us more detail of your spec.

    1) Do you want to devide a selected text(e.g. "cat dog lion") into words?
    2) and want to get the last word "lion"?
    3) If so, what is the delimiter string(e.g. space, comma, etc)?

    Regards,
    Tuesday, June 30, 2015 11:56 AM
  • I want get all ranges of selected text elements. For example, in the string "test test test test", where 1-st and 3-rd word are selected, I should get two ranges: [0,4] and [10,14].
    Tuesday, June 30, 2015 12:08 PM
  • Hi, 
    I'd like to ask you again. What is a "Delimiter" string?  What string will you devide into word?  Is it one space?

    # Your're a student and doing your homework?  I should not provide some code?
    Tuesday, June 30, 2015 12:27 PM
  • I want find all text which selected in the word document.  Delimeter string may be any, initial string may be also any. 

    Yes, I'm a student, but it is not homework, it's my own project. No, you shouldn't.

    Tuesday, June 30, 2015 1:04 PM
  • Hi Ya

    Unfortunately, the Word object model does not provide a way to do what you wish. It's something Word developers have wished for since the capability to make multiple selections was introduced, but it's not exposed to the object model.

    This is something that might become part of the "Word app" (newly designated Word add-in) APIs for coming versions of Word, but is not currently available.

    The closest you can get would be to use the COPY method: Selection.Copy. Then paste this somewhere (a new document, for example). The entire selection will be pasted, with a paragraph mark (ANSI 13) separating each selection. Perhaps you can do something with that...


    Cindy Meister, VSTO/Word MVP, my blog

    • Proposed as answer by L.HlModerator Wednesday, July 1, 2015 3:36 AM
    • Marked as answer by L.HlModerator Tuesday, July 7, 2015 5:50 AM
    Tuesday, June 30, 2015 3:39 PM
    Moderator
  • Thank you for help. I understood.
    Tuesday, June 30, 2015 5:32 PM
  • Ashidacchi, thank you for help too.
    Tuesday, June 30, 2015 5:34 PM