Content control is one of the cool features in Microsoft Word 2010 (first introduced in 2007). Imaging cases, where a user submits their monthly/yearly report by MS Word file. Those files definitely contain quantitative & qualitative data that need to be entered into the main system, used by the company, for review and future planning. Generally, there are data entry people who entered these values manually; but with very minimal effort we can automate the process.We can give template MS Word file to those users which they will fill up and submit each month/year. In the template file, we will have content controls (textbox, drop down list etc). Programmatically we can extract and process these values very easily. And then we will enter these values into the main system without any delay.
To accomplish this, first enable the developer tab: (Steps from MS):
- Start the application.
- Click the Filetab.
- Click Options.
- In the categories pane, click Customize Ribbon.
- In the list of main tabs, select Developer.
- Click OK to close the Options dialog box.
Create your template by adding controls:
- Make sure you are at Developer tab (Marked as )
- You can drag and drop control from Ribbon (Marked as ) and set properties.
- Set a meaningful Title (Marked as ) which you can use in code.
- In case of drop down list add possible value (Marked as ).
- You can check the “Content control cannot be deleted” (Marked as ) to make sure user cannot delete the controls by mistake.
Make sure you are getting both SdtRun and SdtBlock objects to read entire content controls. To understand the Open XML Object Model you can go through several articles but you must use “Open XML SDK 2.0 Productivity Tool” to explore the file by yourself. It will speed-up the learning process. Here is a screenshot of the tools (exploring SampleSale.docx file)
Feel free to leave any comment 🙂
SampleSale.docx | Code.