English 中文(简体)
How to add content control in a Word 2007 document using OpenXML
原标题:

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???

最佳回答

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?

问题回答

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.





相关问题
Call Excel COM Addins functions using macro

How can I call a function defined in a COM Addin using excel macro ? Basically I have created a Excel COM Addin which does some bunch of stuff. Now I want to invoke this via excel macros. Is it ...

Stylesheet of Office2007

Where from I get a stylesheet of Office 2007 Blue for my controls like button, Dropdown list, gridview etc for my ASP.NET Application.

Command line switch for Word that will set the SaveAs Path

I am looking for the switch that will set the default SaveAs path for a word document when calling it from the command line. It seems like the /p parameter exists for Excel but I haven t found any ...

Access Document Stream from Office Ribbon Code

I am attempting to write an add-in for Microsoft Word 2007. I d like to create a Ribbon Button that when clicked, gets a FileStream for the current or active document. I would assume that is possible ...

Hyperlink disappears when form template saved

I have an InfoPath form with several fields on, the problem is that one of the fields is a hyperlink and every time I save the form template, close it and open it again the hyperlink is gone. I know ...

热门标签