The Office Open XML Package specification defines a set of XML files that contain the content and define the relationships for all of the document parts stored in a single package. These packages combine the parts that comprise the document files for Microsoft® Office Excel® 2007, Microsoft Office PowerPoint® 2007, and Microsoft Office Word 2007. The Open XML Application Programming Interface (API) allows you to create packages and manipulate the files that comprise the packages. This topic walks through the code and steps to replace the contents of a document part (file) in an Office Open XML package with other content in Office Word 2007, although the steps are the same for each of the three 2007Microsoft Office system programs that support the Office Open XML Format.
Note: |
|---|
|
The code samples in this topic are in Microsoft Visual Basic .NET and Microsoft Visual C#. You can use them in an add-in created in Microsoft Visual Studio 2008. For more information about how to create an add-in in Visual Studio 2008see Getting Started with the Open XML Format SDK 1.0.
|
Replacing a Package Part with Another Document Part
In the following code, you search and replace a text value stored in the content of a MainDocumentPart part in a WordprocessingDocument package using regular expressions.
' How to search and replace content in a document part.
Public Sub SearchAndReplace(ByVal document As String)
Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, true)
Dim docText As String = Nothing
Dim sr As StreamReader = New StreamReader(wordDoc.MainDocumentPart.GetStream)
docText = sr.ReadToEnd
sr.Close
Dim regexText As Regex = New Regex("Hello world!")
docText = regexText.Replace(docText, "Hi Everyone!")
Dim sw As StreamWriter = New StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))
sw.Write(docText)
sw.Close
wordDoc.Save
End Sub
// How to search and replace content in a document part.
public static void SearchAndReplace(string document)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("Hello world!");
docText = regexText.Replace(docText, "Hi Everyone!");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
}
To search and replace a text value stored in the content of a MainDocumentPart part by using regular expressions
-
First, pass in parameters representing the path to and name of the source Word 2007 document and the name and path of a document part containing theme content.
-
Then, open the document as a WordprocessingDocument object.
-
Next, define regular expressions to search and replace text stored in the MainDocumentPart part of the WordProcessingDocument package.
-
Finally, read the content of the original document file and write it to the new part by using a StreamReader object and a StreamWriter object, respectively.