none
Word 2013 - accessing Style property of Range object causes current selection to appear as no longer selected RRS feed

  • Question

  • This is more of a heads up to other Word 2013 developers than anything... a strange new behavior with Word 2013 that you should be aware of if you have any code that uses the Style property of a Range. 

    When you set a Range object in a Word document and then access the Style property of the range, your current selection in the document appears to become de-selected. At which point, if you begin typing, you'll be overtyping the selected text (which you cannot tell is selected).

    For instance:

    Dim oRange As Word.Range
    Set oRange = ActiveDocument.Paragraphs(1).Range
    Set oStyle = oRange.Style

    You do not need to do anything more than this to see the behavior, although you can add the following to demonstrate what a typical macro of this nature might do next:
    Debug.Print oStyle.NameLocal

    In order to see this happen, you must be in Word 2013 (not 2010), and have some text selected in the document when you run the macro. It is best to hook up the macro to a keyboard shortcut to make it easier to repro/test this.

    This appears to be a screen refresh problem, as doing a ScreenRefresh right after your code runs does make the selection appear visible again. It does not happen if accessing the Style via the selection (i.e., Selection.Range.Style). A lot of my macros use the Range object, and not the Selection object, which is why I noticed this.

    Note: The example above is in VBA for ease of repro'ing, but it also happens in a VSTO add-in written in VB.Net.

    Friday, August 15, 2014 7:44 PM

Answers

All replies

  • Hi PGKW,

    Thanks for the feedback about Office.

    I have reproduced this issue in Office 2013. And since this issue is complex, I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, August 18, 2014 8:31 AM
    Moderator
  • This is a known issue. Please see the blog entry

    http://blogs.msmvps.com/wordmeister/2014/02/26/bug2013-selection-disappears/


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, August 18, 2014 3:32 PM
    Moderator
  • Hi pgkw,

    I do see the issue occurring with 2013, but as a workaround you could try below approach and decide upon,

    Workaround#1) Set sel = Application.Selection

       ' Place your logic here or problematic code

        Selection.SetRange Start:=Selection.Start, End:=sel.End

    Workaround#2) Add 'Application.ScreenRefresh' as a last line to your method, this would bring back selection.

    Hope this helps.

    Monday, August 18, 2014 9:04 PM