none
Word 2010 unexpected results when preforming a "Redo" operation RRS feed

  • Question

  • Good morning

    I have the following code which inserts a content control with inner text matching a selected tree view item in a custom vsto add-in task pane:

                XmlElement selectedElement = (XmlElement)treeView1.SelectedItem;
     
                if (selectedElement != null && !selectedElement.HasChildNodes)
                {
                    CurrentApplication.UndoRecord.StartCustomRecord();
     
                    string selectedElementValue = selectedElement.GetAttribute("name");
     
                    Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;
                    CurrRange.Text = selectedElementValue;
     
                    object CurrRangeObj = CurrRange;
                    CurrRange.Select();
                    Microsoft.Office.Interop.Word.ContentControl cntCtrl = Globals.ThisAddIn.Application.ActiveDocument.ContentControls.Add();
                    cntCtrl.Tag = selectedElementValue;
                    cntCtrl.Title = "Rep_" + cntCtrl.Tag;
     
                    //3 = Dotted Border
                    cntCtrl.Range.Borders.Enable = 3;
     
                    CurrRange.Start = CurrRange.End + 1;
                    CurrRange.Select();
     
                    CurrentApplication.UndoRecord.EndCustomRecord();
                }

    As can be observed the insert is warpped around these two statements:

                    CurrentApplication.UndoRecord.StartCustomRecord();
                    CurrentApplication.UndoRecord.EndCustomRecord();
    This is done so that if a user inserts a content control and then presses undo the WHOLE record is removed.
    The above works correct, the problem I'm having is the following :
    If I insert a content control and then immidiately afterwards press the "Redo" button, 
    the whole line where the content control is present gets a dotted border
    Is there any work around to prevent this from happening?

     


    • Edited by Denys W Tuesday, January 3, 2012 8:23 AM
    Tuesday, January 3, 2012 8:19 AM

Answers

  • Hello Cindy

    I ended up using GDI+ to draw text inside a rectangle(used to mimic the border) and setting the generated image to a picture content control

    Monday, January 9, 2012 10:55 AM

All replies

  • Am not sure if the Undo/Redo is causing a problem or it is the way the graphic/screen looks when implementing this Redo action. Have you tried moving the .Select() statement after the EndCustomRecord() this may assist.

    Regards


    Mike Walker MVP - Visual Developer VSTO 2004-2010 Please mark the best replies as Answers Twitter: www.twitter.com/mikewalker74
    Tuesday, January 3, 2012 1:19 PM
    Moderator
  • Hi Mike

    Thanks for the reply.

    Yes I've tried that, getting the same result though.

    I've also tried placing the line of code which sets the border into a separate undo block but that did not help either

     

    Tuesday, January 3, 2012 1:31 PM
  • Good morning

    Is there anyone who can assist with this issue?

    Any help is appreciated

    Wednesday, January 4, 2012 8:02 AM
  • Hi Denys

    Mmm, difficult, without seeing the display of non-printing characters.

    It looks like the content control may be the only thing in the paragraph? So it's directly adjacent to the paragraph mark?

    My guess would be that this is taking formatting applied to the content control and extending it to the paragraph mark. Word tends to do this when you work in the UI and Redo may be "closer" to the UI than your code. I've no idea how Undo/Redo stores the commands it uses and it's possible that you'd see the problem even if you didn't "group" the commands.

    The only thing I can suggest, besides what you've already attempted, would be to put some characters between the content control and the paragraph mark. You could try a space, for starters. If that doesn't help, try an entire word. I know you don't want that, but for testing purposes, to see if that does solve the problem...


    Cindy Meister, VSTO/Word MVP
    Monday, January 9, 2012 8:07 AM
    Moderator
  • Hello Cindy

    I ended up using GDI+ to draw text inside a rectangle(used to mimic the border) and setting the generated image to a picture content control

    Monday, January 9, 2012 10:55 AM