Removing and Adding controls in the same name

Hi,

We are working on Word Customization using VSTO 2005 and binded XML Schema.

At runtime we are adding labels inside a tag like this,

this.Controls.AddLinkLabel(nodeRange, 50, 10,"labelname1")

At some point we need to remove the label in the document and we are doing it like this,

this.Controls.Remove("labelname1")

and it is removed. Again in some cases we need to recreate the label again which was removed previously by like this

this.Controls.AddLinkLabel(nodeRange, 50, 10,"labelname1")

It throws an error saying that

{Microsoft.Office.Tools.ControlNameAlreadyExistsException: The control cannot be added because a control with the name labelname1already exists in the Controls collection.
at Microsoft.Office.Tools.Word.ControlCollection.CheckIfValidName(String name)
at Microsoft.Office.Tools.Word.ControlCollection.CreateInlineWrapperAndGetCookie(String name, Range range, Object& outObject)
at Microsoft.Office.Tools.Word.ControlCollection.CreateInlineWrapperAndGetCookie(String name, Range range)
at Microsoft.Office.Tools.Word.ControlCollection.InternalInlineAdd[T](Range range, Single width, Single height, String name)
at Microsoft.Office.Tools.Word.ControlCollection.AddLabel(Range range, Single width, Single height, String name)

Though it is removed from the control collection the exception ControlNameAlreadyExists comes.

Any pointers would be a great help.

Regards,

Jeevanantham



Answer this question

Removing and Adding controls in the same name

  • Bob DuPuy

    No, i am not able to solve it.

    What i did was capturing that specific COM exception in Catch block and use Remove method to delete the control again and made a recursive call to Add the control on that particular name.

    The above said was working fine, but it is not the best practice. One thing what you can do is, check whether the Remove method is being properly called.

    Thanks,

    Jeevanantham.S


  • skinned_knuckles

    Hi,

    I have got the same problem, with Office SP2 and all hotfixes. Did you manage to solve it

  • Ha Duo

    hi,Jeevanantham

    i wrote some code to test it, but could not get your result. i think, it is really that you add control in same name after removing it from control collection

    if it is true, i think, you could not get the ControlNameAlreadyExistsException

    best regards

    Daemon Lin


  • j.

    Hi Jeevanatham,

    I know the base case works and while I'm not certain whether we tested inserting controls in XML tags, I can't think of why it would be different.  Oddly enough, from a quick glance at the code, it would appear that there is no case where Remove would not result in the name being removed from the list.  The only thing I can think of would be that Remove is throwing somewhere and somehow that exception is getting eaten--though I can't think of where that would happen.  You might try turning on CLR exceptions in the debugger to see if this is the case.

    The only other thing I can think of would be to try this on an Office SP2 machine (if you aren't running on one already).  We did take some control fixes for Word in that SP, so perhaps this area woudl be affected.

    In any event, this sounds like it might be a bug.  If you wouldn't mind filing it at: http://lab.msdn.microsoft.com/ProductFeedback/reportbug/search.aspx we'll be able to take a look.

    Sincerely,

    Geoff Darst

    Microsoft VSTO Team

     


  • Removing and Adding controls in the same name