none
Dragging text inside ContentControls [VSTO 3.0 + Visual Studio 2008 (C#) on Word 2007 Add-In] RRS feed

  • Question

  • Hello everyone,

     

    I am using Visual Studio Tools for Office 3.0 and Visual Studio 2008 C#. I am developing an Add-In for Word 2007.I have found a strange behavior.

     

    I am attaching to the current events then the document opens:

     

    WordTools.Document vstoDocument = this.Application.ActiveDocument.GetVstoObject();
    
    vstoDocument.ContentControlOnEnter += new DocumentEvents2_ContentControlOnEnterEventHandler(VstoDocumentContentControlOnEnter);
    
    vstoDocument.ContentControlOnExit += new DocumentEvents2_ContentControlOnExitEventHandler(VstoDocumentContentControlOnExit);
    

     

     

    I insert a contentControl (plaintext,richtextbox,building block gallery) in the document using the Developer Tab. I put brake points in the entering and exiting events. I place the cursonr inside the contentcontrol, and the Entering brakepoint gets activated. I place the cursor outside the contentcontrol and the Exiting brakepoint gets activated. Everything OK until now.

     

    The strange behavior starts now:

    Type some text in the document, outside a contentcontrol.Drag and drop the text inside the control. => the entering event will not be triggered.The same thing goes if you have 2 content controls and you drag and drop text from one to another. The exiting event from the source of the drag, end the entering event from the source of the drop will not be triggered.

     

    Can anyone tell me if he encountered this problem and maybe a solution?

     

    Best regards,

    Silviu.

    Wednesday, April 6, 2011 11:19 AM

Answers

  • Hi Silviu

    I confirm the problem. I also see it with Word 2010.

    There are a number of issues with content control events. Some of them were fixed in 2010, but apparently not this one.

    What does appear to work is

    - map the content control to a custom xml part (this won't work for rich text or building block content controls)
    - trap the ContentControlBeforeStoreUpdate event. This event will trigger when text is dragged into the content control


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lupu Silviu Thursday, April 14, 2011 5:29 AM
    Wednesday, April 6, 2011 1:38 PM
    Moderator

All replies

  • Hi Silviu

    I confirm the problem. I also see it with Word 2010.

    There are a number of issues with content control events. Some of them were fixed in 2010, but apparently not this one.

    What does appear to work is

    - map the content control to a custom xml part (this won't work for rich text or building block content controls)
    - trap the ContentControlBeforeStoreUpdate event. This event will trigger when text is dragged into the content control


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lupu Silviu Thursday, April 14, 2011 5:29 AM
    Wednesday, April 6, 2011 1:38 PM
    Moderator
  • Hello Meister,

     

    Thank you for confirming my fears :P . Unfortunately, I need it working on rich text and building blocks. Is there I way I could report this bug to Microsoft?

     

    Best regards,

    Silviu.

     

    Wednesday, April 6, 2011 3:06 PM
  • Hi Silviu

    I've passed the issue along to someone I know at the product group, but won't get any reply/discussion/workaround. It will be in the database and get looked at - for future versions.

    On other workaround suggestion I can offer is to use the WindowSelectionChange event. This is an application level event that fires whenever the user uses arrow keys or the mouse (and a couple of other things) to move the cursor inside a document. I just tested and it does trigger with drag-and-drop. To find out whether the target range is within a content control you can compare the Range of the event's Sel argument with the Range of a content control:

        Dim ctl As word.ContentControl = Sel.Document.Contentcontrols(1)
        If Sel.Range.InRange(ctl.Range) Then Debug.Print "Enter"

    But if you need further support on finding a workaround then the only thing you can do is use paid support...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lupu Silviu Thursday, April 7, 2011 8:59 AM
    • Unmarked as answer by Lupu Silviu Thursday, April 14, 2011 5:29 AM
    Thursday, April 7, 2011 7:14 AM
    Moderator
  • Hello,

     

    I will give it a try, and homefully I will be able to fix my problem. I will come back with the results. Thank you again.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Thursday, April 7, 2011 7:22 AM
  • Hello,

     

    Found another strange thing. If you select the hole text from inside a ContentControl, and drag it, it will copy the contentcontrol also. True, when dropping, the Paste menu will appear, and there is an option that asks if you want to keep the control or not.

    Is there a way to prevent the contentcontrols from being copied in the situation presented above?

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Monday, April 11, 2011 12:47 PM
  • Hi Silviu

    This is known behavior and "by design". It's never been clear to me why it was designed this way...

    The only way I know is to place some kind of protection on the document, either by grouping the content controls or forms protection, or "Read-only" protection.


    Cindy Meister, VSTO/Word MVP
    Tuesday, April 12, 2011 8:51 AM
    Moderator
  • Hello Cindy,

     

    I used the ContenControlAfterAdd event, and added some safeguards, and for the time being, everything seems to be ok.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Tuesday, April 12, 2011 9:03 AM
  • I used the ContenControlAfterAdd event, and added some safeguards, and for the time being, everything seems to be ok

    Hi Silviu

    Sounds good! Could you share a bit more detail with us about what you do in the event, please :-)?


    Cindy Meister, VSTO/Word MVP
    Tuesday, April 12, 2011 2:51 PM
    Moderator
  • Hello Cindy,

     

    The fix in possible because I have a certain structure. And in my case, a content control contains some contentcontrols, but those contentcontrols from inside, are not allowed to have anything but text.

     

          try
          {
            if (NewContentControl.ParentContentControl != null && NewContentControl.ParentContentControl.ParentContentControl != null)
            {
              NewContentControl.Delete(false);
            }
          }
          catch { }
    

    The rule can be changed, to suite the application. Is important that the Delete method to have False so that upon moving, the text will remain in the desired area.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, April 13, 2011 9:22 AM