Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Placeholders in word document with c#

I have a template in word (.docx) format and want to replace some placeholders in this template with my own data, do you know where I can find the right classes for this project? It would be nice for me to know the namespace. Are there any newer classes for handling word documents in the docx format (xml) than the com classes? Thank you for all your answers, I hope someone can help me with my problem.

greets

Sebastian

like image 943
Sebastian Müller Avatar asked Oct 08 '08 11:10

Sebastian Müller


People also ask

How do you change placeholders in Word?

On the Developer tab, in the Controls group, click Design Mode. Click the content control where you want to revise the placeholder instructional text. Edit the placeholder text and format it any way you want.

How do I find a place holder in Word?

End of dialog window. Step 2: Place cursor in the document where you want to insert a Placeholder. Step 3: Go to the References tab on the Ribbon and click on the Insert Citation drop-down menu in the Citations & Bibliography section. Step 4: Click on the Add New Placeholder option from the drop-down menu.


3 Answers

The new office formats (docx, xlsx, etc) are zip files that contain a collection of xml files. With that in mind you have a couple of approaches.

  1. You can use the Open XML SDK located at http://www.microsoft.com/downloads/details.aspx?FamilyId=AD0B72FB-4A1D-4C52-BDB5-7DD7E816D046&displaylang=en

  2. You can unzip the docx file, do a search and replace for your tokens, and zip it back up.

There is a website at openxmldeveloper.org that is just for this kind of thing. Also, bear in mind that they are already shipping a beta version 2 of the SDK.

like image 133
NotMe Avatar answered Oct 04 '22 20:10

NotMe


Eric White has touched on exactly this subject in a blog article detailing a program meant to validate source snippets embedded in DocX files. Beyond just that article I highly recommend reading his series on Office Open XML and C#.

like image 25
user7116 Avatar answered Oct 04 '22 20:10

user7116


I used this one:

IEnumerable test2 = from element in body.Elements() where element.InnerText.Contains("sdt") select element;

like image 37
Freek Bos Avatar answered Oct 04 '22 20:10

Freek Bos