views:

857

answers:

3

Hi I want to create a word 2007 document without using object model. So I would prefer to create it using open xml format. So far I have been able to create the document. Now I want to add a content control in it and map it to xml. Can anybody guide me regarding the same???

+2  A: 

Have a look for the Word Content Control Toolkit on www.codeplex.com.

Here is a very brief explanation on how to do what you are attempting.

  1. You need to have access to the developer tab on the Word ribbon. To get this working click on the Office (Round thingy) in the top left hand corner and Select Word Options at the bottom of the menu. On the first options page there is a checkbox to show the developer toolbar.

  2. Use the developer toolbar to add the Content controls you want on the page. Click the properties button in the Content controls section of the developer bar and set the name and tag properties (I stick to naming the name and tag fields with the same name).

  3. Save and close the word document.

  4. Open the Content control toolkit and then open your document with the toolkit. Use the left hand pain to create some custom xml to link to your controls.

  5. Now use the bind view to drag and drop the mappings between your custom xml and the custom controls that are displayed in the right panel of the toolkit.

  6. You can use the openxml sdk 1.0 or 2.0 (still in ctp) to open your word document in code and access the custom xml file that is contained as part of the word document.

If you want to have a look at how your word document looks as xml. Make a copy of your word document and then rename it to say "a.zip". Double click on the zip file and then navigate the folder structure. The main content of the word document is held under the word folder in a file called "document.xml". The custom xml part of the document is held under the customXml folder and is generally found in the file named "item1.xml".

I hope this brief explanation get you up and running.

Andrew
I want to add content control programmatically using open xml sdk....
Anoop
+2  A: 

Anoop,

You said that you are able to creat the document using OpenXmlSdk. With that assumption, you can use the following code to create the content control to add to the Wordprocessing.Body element of your Document.

//praragraph to be added to the rich text content control
Run run = new Run(new Text("Insert any text Here") { Space = StaticTextConstants.Preserve });
Paragraph paragraph = new Paragraph(run);

SdtProperties sdtPr = new SdtProperties(
        new Alias { Val = "MyContentCotrol" },
        new Tag { Val = "_myContentControl" });
SdtContentBlock sdtCBlock = new SdtContentBlock(paragraph);
SdtBlock sdtBlock = new SdtBlock(sdtPr, sdtCBlock);

//add this content control to the body of the word document
WordprocessingDocument wDoc = WordprocessingDocument.Open(path, true); //path is where your word 2007 file is
Body mBody = wDoc.MainDocumentPart.Document.Body;
mBody.AppendChild(sdtBlock);

wDoc.MainDocumentPart.Document.Save();
wDoc.Dispose();

I hope this answers a part of your question. I did not understand what you ment by "Map it to XML". Did you mean to say you want to create CustomXmlBlock and add the ContentControl to it?

Bijay Kusle
Thanx for the answer!!Though I already got a nice article in MSDN which also explains XML mapping...http://msdn.microsoft.com/en-us/library/dd469465.aspx
Anoop
A: 

Any idea, what should be done if I want to copy a content Control into another, along with its contents. Note: Content control will contain other content controls too. When I copy the content control, I need to copy it along with its contents

Krishnan