in .NET Framework

Modify a Word 2007 Open XML document via C#

Interesting problem that I came across here…A client requested that their system was to have the ability to load in a Word 2007 Document, perform some text manipulation routines and save the document back to the server.

Firstly, you can rename a docx document to and you can extract all of the files in the zip file that build the foundation of the Open XML document.   Not bad!

Here is the code that I used to open a Word 2007 document, do some manipulation and save the file.

using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(newPath, true))

string docText;
using (var sr = new StreamReader(wdDoc.MainDocumentPart.GetStream()))
docText = sr.ReadToEnd();

docText = RegExpReplace(“{REPLACETEXTHERE}”, docText, “With this value”);
using (var sw = new StreamWriter(wdDoc.MainDocumentPart.GetStream(FileMode.Create)))


The WordprocessingDocument classes are part of the Open XML Format 1.0 SDK available from Microsoft.

This beats having to do the old way of importing Office API Dlls via COM to load and write word documents!