none
Delete Content control from ContentControlBeforeDelete event RRS feed

  • Question

  • Hello,

     

    I am developing a VSTO Add-In for Word 2007. I am trying to add a code that deletes dome content controls (C2,C3), then a certain contentcontrol(C1) is deleted.

    When C1 is deleted, the "ContentControlBeforeDelete" event on the document is triggered, then that is where I wrote my code. But when I try to do C2.Delete(true) and C3.Delete(true), I get some errors like "Index refers beyond end of list." or "Exception returned while ... COM-Exception".

     

    Does someone know how to solve this? Is it possible to delete ContentControls in such a way?

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    • Edited by Lupu Silviu Wednesday, October 12, 2011 6:59 AM
    Wednesday, October 12, 2011 6:58 AM

Answers

  • Hi Silviu

    Basic rule of thumb: you can't manipulate content controls during a content control event. Try instantiating a Timer that will execute after the event finishes.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lupu Silviu Wednesday, October 12, 2011 12:35 PM
    Wednesday, October 12, 2011 11:48 AM
    Moderator

All replies

  • Hi Silviu

    Basic rule of thumb: you can't manipulate content controls during a content control event. Try instantiating a Timer that will execute after the event finishes.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lupu Silviu Wednesday, October 12, 2011 12:35 PM
    Wednesday, October 12, 2011 11:48 AM
    Moderator
  • Hi Lupu,

    If you're trying to do this by iterating thru the content control collection of the document you can get your index out of range. When deleting something from a collection, often you have to perform it in a reverse order ... meaning starting from the end of the collection and working backwards.

    Here's a sample from a program I'm currently working on where the collection are user selected items (they can be multiple selections) from a list box. If selected I want to delete the selected item and I have to use the index of that item for clearing an array of data about the listbox item.

                For i = Me.ListBox2.SelectedIndices.Count - 1 To 0 Step -1
                    index = Me.ListBox2.SelectedIndices(i)
                    Me.ListBox2.Items.Remove(Me.ListBox2.Items(index))
                    ListArray2(index) = "zzzz"
                    x = index
                    Do Until index = listCount
                        x = x + 1
                        If x = 1000 Then
                            x = 999
                            ListArray2(x) = Nothing
                        End If
                        ListArray2(index) = ListArray2(x)
                        index = index + 1
                    Loop
                    listCount = Me.ListBox2.Items.Count
                Next
    
    

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com
    Wednesday, October 12, 2011 12:07 PM
  • Hello,

     

    A timer and a private variable did the trick. Now I have to think of possible Race conditions :( . At least it works. Thanks both of you for the replies.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, October 12, 2011 12:35 PM