VSTO Word ContentControls, Y U No have Name property?

Posted by System.Cats.Lol on Stack Overflow See other posts from Stack Overflow or by System.Cats.Lol
Published on 2011-02-18T22:37:41Z Indexed on 2011/02/18 23:25 UTC
Read the original article Hit count: 271

Filed under:
|
|
|
|

When you add a VSTO (not Word native) content control, you specify the name:

controls.AddContentControl(wordRange, "foo", wdType);

Where controls is the VSTO (extended) Document.Controls collection.

You can later look up the control by name:

ContentControl myContentControl = controls["foo"];

So why in the world is there no Name property for ContentControl? (or ContentControlBase, or any of the other derivatives).

I'm implementing a wrapper class for the Document.Controls property that lets you add or iterate the content controls. When iterating the underlying Document.Controls, there's no way to look up the name of each control. (We need it to return an instance of our ContentControl wrapper). So currently I'm doing this in our ContentControls wrapper class:

    public IEnumerator<IContentControl> GetEnumerator()
    {
        System.Collections.IEnumerator en = this.wordControls.GetEnumerator();
        while (en.MoveNext())
        {
            // VSTO Document.Controls includes all managed controls, not just 
            // VSTO ContentControls; return only those.
            if (en.Current is Microsoft.Office.Tools.Word.ContentControl)
            {
                // The control's name isn't stored with the control, only when it was added,
                // so use a placeholder name for the wrapper.
                yield return new ContentControl("Unknown", (Microsoft.Office.Tools.Word.ContentControl)en.Current);
            }
        }
    }

I'd prefer to not have to resort to keeping a map of names-to-wrapper-objects in our ContentControls object. Can anyone tell me how to get the control's name (the name parameter that was passed to Controls.Add()?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET