none
Table and inline shapes drag'n'drop problem. RRS feed

  • Question

  • Hello. I'm developing an application-level Word 2010 addin using VSTO (.NET framework 4.0). In my addin I widely use a tables and inline shapes (embedded Excel charts). For these objects, I created a special wrapper classes that implement all business logic. References to tables and inline shapes are stored interior of these classes. But after dragging these references no longer be valid - if you attempt to access any property COMException ("Object has been deleted") appears. I need to solve this problem. I tried the following solutions:

    1)       Using a timer. However, this is a very unreliable way.

    2)       Using a RichTextContentControl as a container for tables and inline shapes. But user can extract inline shape from the control by dragging it from. Besides, it looks unaesthetic. Is there a way to fill in all the control with inline shape and prohibit dragging inline shape?

    Also, I am considering writing a special ActiveX (MFC) controls. But after a few futile attempts I was unable to insert a table inside the control. Is this possible?

    • Edited by Tenere Tuesday, January 24, 2012 5:46 PM
    Tuesday, January 24, 2012 5:44 PM

Answers

  • Hi Tenere

    When you use the mouse to drag something, what actually happens is a lot like if you'd use Cut/Paste via the Clipboard: the object is destroyed in the UI then reconstructed. That's why you're losing the references to the objects.

    Unfortunately, there's no way to "tag" or "name" Tables or InlineShapes reliably and permanently.

    With a table you could insert a Bookmark at the beginning of the first cell. In that position, the user can hardly delete it by accident. So if the object no longer exists you can loop through the table and can check whetherthe associated bookmark exists in a table: tbl.Range.Bookmarks.Exists("NameOfBookmark")

    Similarly, if you assign a bookmark to an InlineShape's Range it should move with the range when the InlineShape is dragged. So to find a specific InlineShape you can use Document.Bookmarks("NameOfBookmark").Range.InlineShapes(1)

    Note: Code is in VB-speak.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tenere Thursday, January 26, 2012 4:18 PM
    Wednesday, January 25, 2012 5:59 AM
    Moderator

All replies

  • Hi Tenere

    When you use the mouse to drag something, what actually happens is a lot like if you'd use Cut/Paste via the Clipboard: the object is destroyed in the UI then reconstructed. That's why you're losing the references to the objects.

    Unfortunately, there's no way to "tag" or "name" Tables or InlineShapes reliably and permanently.

    With a table you could insert a Bookmark at the beginning of the first cell. In that position, the user can hardly delete it by accident. So if the object no longer exists you can loop through the table and can check whetherthe associated bookmark exists in a table: tbl.Range.Bookmarks.Exists("NameOfBookmark")

    Similarly, if you assign a bookmark to an InlineShape's Range it should move with the range when the InlineShape is dragged. So to find a specific InlineShape you can use Document.Bookmarks("NameOfBookmark").Range.InlineShapes(1)

    Note: Code is in VB-speak.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tenere Thursday, January 26, 2012 4:18 PM
    Wednesday, January 25, 2012 5:59 AM
    Moderator
  • Thank you, Cindy. I decided to use the Content controls as containers for my objects (tables, charts, etc.). To determine whether the control is dragged or deleted, I use a timer that is activated in the control's Deleting handler.


    • Edited by Tenere Thursday, January 26, 2012 4:18 PM
    Thursday, January 26, 2012 4:18 PM