Editor’s note: The following post was written by Visual C# MVP Ming Man Chan

Referring to Content Control using C#

For the Microsoft Office developers before Office 2010, they will be familiar with ActiveX control. Microsoft Office has come out with a Content Control since Office 2010.  How the controls are referenced is different from the ActiveX control. This article will show you how to refer to it. 

This article has 3 sections to illustrate how to referring to Content Control using C#. 

  • Create an Office project using the Word template.
  • Add the Content Control into the page.
  • Referring the Content Control using C# and change the content of the control.

 Create an Office project using the Word template

 1. Click on File  -> Project... -> New Project.

 

2. Choose Word 2013 Document in Office / SharePoint template.

 

3. Select the docx then click in Visual Studio Tools for Office Project Wizard leave others as default then click OK.

 

Add the Content Control into the page.

1. Type something in the Word document page for example, “Here is Content Control”.

 

2. Click on the Developer tab in the Word Document then click on the Plain Text Content Control and click below the text you typed just now.

 

If you do not see the Developer tab then do this How to: Show the Developer Tab on the Ribbon http://msdn.microsoft.com/en-us/library/bb608625.aspx.

Referring the Content Control using C# and change the content of the control

 

1. Click on the Properties tab on the left of the Visual Studio.

2. Click on the Events (button).

 

3. Click on ContentControlOnExit ().

 

4. Type the below code in ThisDocument_ContentControlOnExit event.

Microsoft.Office.Interop.Word.ContentControl cc = this.Application.ActiveDocument.ContentControls[1];

cc.Range.Text = "I can run";

 

5. Run the Word application from Visual Studio by pressing F5. Click the Content Control.

 

6. Click outside the Content Control then you will see your code effect.

 

Reference:

How to: Add Content Controls to Word Documents (http://msdn.microsoft.com/en-us/library/bb386200.aspx)

 

About the author

 

Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000.  He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.  Additionally you can read Ming’s Channingham’s blog

 About MVP Monday

The MVP Monday Series is created by Melissa Travers. In this series we work to provide readers with a guest post from an MVP every Monday. Melissa is a Community Program Manager, formerly known as MVP Lead, for Messaging and Collaboration (Exchange, Lync, Office 365 and SharePoint) and Microsoft Dynamics in the US. She began her career at Microsoft as an Exchange Support Engineer and has been working with the technical community in some capacity for almost a decade. In her spare time she enjoys going to the gym, shopping for handbags, watching period and fantasy dramas, and spending time with her children and miniature Dachshund. Melissa lives in North Carolina and works out of the Microsoft Charlotte office.