none
C# - Add content control to textbox in Word 2010

    Question

  • Hello,

    I wanted to know if it is possible to add content control to textboxes. So when the user clicks on a button, it inserts a textbox with content control around it. That way, when I add the Hide functionality, the textbox along with the text inside the box hides. Right now I have content controls just by themselves and I can show/hide them. But to add them to a shape seems a little tricky since the user would be adding multiple textboxes (which would by default be placed at the same location).

    All that I have right now is the code for adding a textbox.

    WRange = currentSelection.Range;
                Word.Document WDocument = application.ActiveDocument;
                WDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 100, 75, 150, 100, WRange);
                WDocument.ContentControls.Add(Word.WdContentControlType.wdContentControlText, WRange);

    I'm using C# and Word 2010. Any help will be appreciated.

    Thanks,

    May

    Thursday, December 26, 2013 6:36 PM

Answers

  • Hi May

    First thing to do is to see if what you want is possible in the Word UI. Insert a text box, see if you can insert a content control into it, then see if making the text box invisible affects the content control. If you can do it in Word, you can do it with code.

    To get the "internal range" of a text box (target for adding the content control), which will be independent of the main body of text, you need to assign the text box to a Word.Shape object as part of the AddTextbox method (Word.Shape shpTB = Document.Shapes.AddTextbox()). Then, Word.Range rngTB = shpTB.TextFrame.TextRange


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 27, 2013 9:01 AM