How to add an item to an existing dropdownlistcontentcontrol in the Active document RRS feed

  • Question

  • In my VSTO application level word addin, Added a dropdownlist contentcontrol with search link in it. searchlink will gives a pop up to select the value.After selecting the value i need add the selected item to the dropdown list.While doing it is throwing an exception.
    This is my code

    // add the selected item to the dropdown list

    `Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                                    foreach (Word.ContentControl cc in doc.ContentControls)

                                                Word.ContentControlListEntry listEntry2 = cc.DropdownListEntries.Add(searchValues.ValueMeaning, searchValues.ValueCode);
    Monday, December 9, 2019 11:41 AM

All replies

  • Is your exception a System.Runtime.InteropServices.COMException? If so, what description is being displayed?

    Just a guess, but since you are looping round content controls, the first thing I would check is that the content control you are trying to modify is actually a DropDownList or ComboBox control.

    Peter Jamieson

    Wednesday, December 11, 2019 11:50 AM
  • Thanks for the reply.

    Discription for that error is " client.WordAddin.ThisAddIn.ActiveDocument_ContentControlBeforeStoreUpdate:NS_2111_VARLOV_SMC297,1: Exception in ActiveDocument_ContentControlBeforeStoreUpdate 
    This object model command is not available while in the current event. " 

    Contentcontrol which i am trying to modify is DropDownList.

    Please let me know what is wrong with the code

    • Edited by Aparna_K Thursday, December 12, 2019 11:34 AM
    Thursday, December 12, 2019 9:10 AM
  • The problem is that somewhere in your code you have registered a routine that responds to the ContentControlBeforeStoreUpdate event.

    So I will guess that what you have is a content control that is mapped to an element or attribute in a Custom XML Part, and you want to use that Event to respond to a change in the value of Control by modifying the value of another Content Control (the DropdownList).

    However, although Word lets you access many parts of its object model when you are processing that event, it just won't let you modify the value of a content control. So you cannot easily use that event in the way that you want.

    Instead, I suggest you use the Events available in the CustomXmlPart object (NodeAfterInsert, NodeAfterDelete, NodeAfterReplace. Roughly speaking, you may have to code all three events because if the user deletes all the text in a Content Control, you'll get a NodeAfterDelete event. If they then put some text into the control, you'll get a NodeAfterInsert event, and if they modify the existing text you'll get a NodeAfterReplace event.

    But these events *should* allow you to modify the DropDownList control.

    (There is another way to handle this which is described in Greg Maxey's page at (it's all VBA so you would need to translate to C#). There could well be a reason why he chose that route rather than relying on the CustomXmlPart events, but if so I either never knew or cannot remember what it was. As far as I can see, using the CustomXmlPart events is cleaner and simpler.

    Peter Jamieson

    Thursday, December 12, 2019 11:51 AM
  • Excellent..It worked , Added my logic( Word.ContentControlListEntry listEntry2 = cc.DropdownListEntries.Add(searchValues.ValueMeaning, searchValues.ValueCode); ) in NodeAfterReplace method..Thank you so much for your suggestion.
    Friday, December 13, 2019 10:19 AM