in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

This Blog

Syndication

All About SharePoint - S.S. Ahmed - MVP Microsoft SharePoint

All About SharePoint, as the name suggests, is all about SharePoint. It has articles, tutorials, source code, FAQs, and tips about SharePoint, InfoPath, C#, Microsoft Office, SQL Server, XML, etc.

Adding content to Word "content control" programmatically

Category: Word programming

Level: Advance 

Here is a small tip on how to add data to Word content control programmatically. If you don't know what are content controls, you can read about them at the following sites:

 http://blogs.msdn.com/microsoft_office_word/archive/2006/10/23/control-yourself.aspx

http://blogs.msdn.com/modonovan/archive/2006/05/23/604704.aspx

http://blogs.msdn.com/erikaehrli/archive/2006/08/11/word2007DataDocumentGenerationPart1.aspx

http://msdn2.microsoft.com/en-us/library/ms406053.aspx

The articles above will give you a clear picture about content controls. The beauty of content controls is that these can be populated with data programmatically. Of course, this could be done in different ways. Following is one way of doing it:

object contentcontrolIndex;
                
contentcontrolIndex = 1; //index of content controls always start from 1, not from 0!!
                
Word.ContentControl contentcontrolSample = this.Application.ActiveDocument.ContentControls.get_Item(ref contentcontrolIndex);

contentcontrolSample.Range.InsertAfter("This is a test!"); 

"this" in third line points to the current open document. When trying this code, replace "this.Application" with your own Word reference. If you want to learn more about this topic, read the following article (VSTO 2005 SE Sample):

http://www.microsoft.com/downloads/details.aspx?familyid=9331eaa4-fc4f-49e1-ac78-aa2f112d854e&displaylang=en

The code above assumes there is only one content control in the application. The index of the control will be 1. Using this technique, you can populate content controls with data retrieved from different data sources including databases and other documents.

-SSA

 

 

Comments

 

Blogger Loser » Blog Archive » Adding content to Word “content control” programmatically said:

Pingback from  Blogger Loser  » Blog Archive   » Adding content to Word “content control” programmatically

September 7, 2007 5:54 AM
 

warraich said:

Just wondering if you could help me out please, I am trying to get data from Word Document (2007) through XML or any other mean (content controls) I used content controls and got the Xml file but I lose formating and all that. 'd be great if I had just the plain text but. Just wondering if you 'd have any advice.

Thanks much.

warraich@ur.msu.edu

March 11, 2008 12:52 PM
 

ssa said:

Dear Warraich

I dont know of any easy solution that would give you the word content along with all the formatting one'd have done in the word itself. Simple text can be extracted using the code. Here is one example:

asptutorials.net/.../accessing-ms-word-documents-from-dot-net

regards

March 22, 2008 10:34 PM
 

Jeremy said:

To Whomever Can Help:

I don't know much about the code side of things, but I'm trying to set up a template in Word 2007 for a proposal. I have richtext controls in it they work fine so far. However, I want to be able to group them or something so that when I open up the document(from the template) and change the first company name richtext control, it will change all the rest of them at once. So for an example, I want to have controls throughout the text of the proposal that will say a company name like 'Microsoft'. But I don't know what that company name will be until proposal writing time. I want to open the document and type in the first control whatever the company is for that proposal, and all the rest that I need will change to that. Get it? Is there a way to do that, other than Find & Replace?

May 29, 2008 1:03 PM
 

Mahesh said:

Hi

I am trying to use Rich Text field in the document library of MOSS to map to rich text content control.

It only maps to plain text.

Is there a way to map. please let me know

thx

regards

mahesh

August 11, 2008 1:27 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

About ssa

MOSS MVP - Over 8 years experience. 4 years SharePoint experience!

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts