none
ActiveWindow.Selection.TextRange does not return the whole text selection when selecting split texts RRS feed

  • Question

  • Hi,

    if you select two different text blocks in a shape in PowerPoint.

    i.e cooking and barbecueing in this example.

    ActiveWindow.Selection.TextRange.Text only returns the lastly selected text.

    i.e. you first select cooking and then barbecueing. The text in the selection is only barbecueing.

    In my estimation it should return cooking and barbecueing as well as two lines if I access the Lines(-1,-1) of the TextRange.

    Can you tell me if my estimation is wrong or if there is a workaround to get the information needed.

    Thanks.

    Best regards

    Christopher Black


    Thursday, November 10, 2016 11:12 AM

Answers

  • Hi Christopher

    I can confirm from my experience that the selection object returns only the last of the selected text areas.

    I do not know what you try to achieve exactly but maybe this would be a feasible workaround:

    First check if some text formatting (e.g. "Shadow", "Strikethrough", ...) is NOT used in the WHOLE text range of the shape. If you find such a format option, run code like this:

    Application.CommandBars.ExecuteMso "Shadow"

    Of course "Shadow" has to be replaced with the ribbon control id of the "not used" text format (for the explanation I continue using "Shadow").

    Now the current text selection (even if multiple text areas are selected) is formatted as shadowed. So you can cycle through the Runs collection of the Shape.Textrange object (which contains ALL text and not just the selected text) and detect the text ranges formatted as shadowed. These ranges will be identical to the original selection. Do whatever you have to do with them. And do not forget to remove the "Shadow" format again.

    To be honest, I never implemented this workaround, but I currently do not see an obvious reason why this should not work.

    Hope this helps. Good luck!

    Cheers, Luca


    Thursday, November 10, 2016 10:08 PM

All replies

  • Hi Christopher

    I can confirm from my experience that the selection object returns only the last of the selected text areas.

    I do not know what you try to achieve exactly but maybe this would be a feasible workaround:

    First check if some text formatting (e.g. "Shadow", "Strikethrough", ...) is NOT used in the WHOLE text range of the shape. If you find such a format option, run code like this:

    Application.CommandBars.ExecuteMso "Shadow"

    Of course "Shadow" has to be replaced with the ribbon control id of the "not used" text format (for the explanation I continue using "Shadow").

    Now the current text selection (even if multiple text areas are selected) is formatted as shadowed. So you can cycle through the Runs collection of the Shape.Textrange object (which contains ALL text and not just the selected text) and detect the text ranges formatted as shadowed. These ranges will be identical to the original selection. Do whatever you have to do with them. And do not forget to remove the "Shadow" format again.

    To be honest, I never implemented this workaround, but I currently do not see an obvious reason why this should not work.

    Hope this helps. Good luck!

    Cheers, Luca


    Thursday, November 10, 2016 10:08 PM
  • Hi Christopher Black (Germany),

    I try to fetch the split selected text from the shape but did not succeeded.

    so you can try to just get the selection.text or selection.range.text to get whole text.

    then you can try to split from the (¶).

    Sub demo()
    Dim str As String
    str = ActiveWindow.Selection.Range.Text
    Debug.Print (str)
    End Sub

    you will get whole text.

    then you can split like below.

    vData1 = Split(vData, Chr(182))

    you can use loop and store the words in array. so that you can use them individually where you want to use.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 11, 2016 5:08 AM
    Moderator
  • Hi Deepak,

    since we are trying to add custom text formatting to the selected text, we need to know which text was selected.

    Getting the whole text does not really support our use case.

    Best regards

    Christopher Black

    Friday, November 11, 2016 1:42 PM
  • Hi Luca,

    thanks.

    I will have to test your suggestion and check if it is feasible for our use case.

    Best regards

    Christopher Black

    Friday, November 11, 2016 1:43 PM
  • Hi Christopher Black (Germany),

    you had mentioned that you want to apply Text formatting on selected text.

    so that you want to get the text.

    I made a simple test you can check the result.

    I create a shape like yours and add a Text in that. then select split Text.

    Then I add just one line of code to change the font.

    Sub demo()
    Selection.Font.Name = "Tahoma"
    End Sub
    

    when you run the code you will find that font applied for both the part of Text.

    so you can apply the formatting to split text with Selection.text.

    no need to fetch the Text value.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 14, 2016 9:26 AM
    Moderator
  • Hi Deepak

    I guess you kind of misinterpret Christopher's issue. His sample code above shows he is using PowerPoint whereas you code suggestions are Word-based (I guess). There's no issue in Word though.

    Luca

    Monday, November 14, 2016 10:27 AM
  • Hi,

    sorry if I did not add the information that I was using PowerPoint to the question.

    I thought I did, but after reading my question again I have seen it was missing.

    I added it to the question.

    Best regards

    Christopher

    Monday, November 14, 2016 11:12 AM
  • Hi,

    I have tested this workaround to see if we can use this to perform our logic and it worked.

    Thanks.

    Best regards

    Christopher Black

    Monday, November 14, 2016 11:18 AM