none
Accessing Range.WordOpenXML loses Document.Selection? [Word] RRS feed

  • Question

  • Once I access any WordOpenXML on any Range, the current Application.Selection is lost/bashed leaving nothing is selected. I can restore that by using something like:

    // C#
    
    Word.Range origsel = Application.Selection.Range;
    
    string xmlstuff = Application.Selection.Range.WordOpenXML;
    
    origsel.Select();

    but the final Select() call doesn't faithfully restore the original selection. I'm primarily dealing with Selections/Ranges that have both Shape and InlineShape objects, and the Shape objects are not highlighted/selected at the end.

    My use scenario is that I need a list of Shape/InlineShape objects within the current selection when the user issues Cut/Copy. I would strongly prefer that this be seamless/background, but it looks like using Range.WordOpenXML is going to cause a flicker in the highlighting no matter what.

    AFAIK, this is the recommended way to restore the original Selection ... alternatives/suggestions welcome.

    I am currently on VS2013 and Word 2013.



    Saturday, January 13, 2018 11:53 AM

Answers

  • Hello Denton Thomas,

    To avoid flicker, I tried to use Application.ScreenUpdating. In my test, it could also solve the issue that the selected shape would be lost. I would suggest you try to test below code for you.

      private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Application application = Globals.ThisAddIn.Application;
                application.ScreenUpdating = false;
                Word.Range origsel = application.Selection.Range;
    
                string xmlstuff = application.Selection.Range.WordOpenXML;
    
                origsel.Select();
                application.ScreenUpdating = true;
            }

    Best Regards,

    Terry


    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.

    • Marked as answer by Denton Thomas Tuesday, January 16, 2018 1:11 PM
    Monday, January 15, 2018 3:24 AM

All replies

  • Something you could try would be to set a bookmark "around" the Range (Document.Bookmarks.Add), then reselect the bookmark's Range. Not sure if that will be any better, but worth a try.

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Saturday, January 13, 2018 7:03 PM
    Moderator
  • Hello Denton Thomas,

    To avoid flicker, I tried to use Application.ScreenUpdating. In my test, it could also solve the issue that the selected shape would be lost. I would suggest you try to test below code for you.

      private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Application application = Globals.ThisAddIn.Application;
                application.ScreenUpdating = false;
                Word.Range origsel = application.Selection.Range;
    
                string xmlstuff = application.Selection.Range.WordOpenXML;
    
                origsel.Select();
                application.ScreenUpdating = true;
            }

    Best Regards,

    Terry


    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.

    • Marked as answer by Denton Thomas Tuesday, January 16, 2018 1:11 PM
    Monday, January 15, 2018 3:24 AM
  • Ah, thanks so much, both of you.

    Terry - that works exactly as expected. It looks like I can also drop the Selection handling altogether - twiddling ScreenUpdating seems to prevent any changes to Application.Selection. A bit more testing needed, but this looks great.

    Thanks again - many problems resolved with those few lines of code!


    Denton

    Tuesday, January 16, 2018 1:11 PM