Partager via


How to: Replace a Document Part in an Office Open XML Package by Using the Open XML API

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

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 document 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 a document part (file) in an Office Open XML package with another document part in Office Word 2007, although the steps are the same for each of the three 2007 Microsoft Office system programs that support the Office Open XML Format.

NoteNote

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 2008, see Getting Started with the Open XML Format SDK 1.0.

Replacing a Package Part with Another Part

In the following code, you replace one document part in a package with another document part and then add contents to the updated part.

' How to replace a document part in a package.
Public Sub ReplaceTheme(ByVal document As String, ByVal themeFile As String)
   Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, True)
   Using (wordDoc)
      Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
      ' Delete the old document part.
      mainPart.DeletePart(mainPart.ThemePart)
      ' Add a new document part and then add content.
      Dim themePart As ThemePart = mainPart.AddNewPart(Of ThemePart)()
      Dim streamReader As StreamReader = New StreamReader(themeFile)
      Dim streamWriter As StreamWriter = New StreamWriter(themePart.GetStream(FileMode.Create))
      Using (streamWriter)
         streamWriter.Write(streamReader.ReadToEnd)
      End Using
   End Using
End Sub
// How to replace a document part in a package.
public static void ReplaceTheme(string document, string themeFile)
{
   using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
  {
      MainDocumentPart mainPart = wordDoc.MainDocumentPart;
      // Delete the old document part.
      mainPart.DeletePart(mainPart.ThemePart);
      // Add a new document part and then add content.
      ThemePart themePart = mainPart.AddNewPart<ThemePart>( );

      using (StreamReader streamReader = new StreamReader(themeFile))
      using (StreamWriter streamWriter = new StreamWriter( themePart.GetStream(FileMode.Create ) ))
         {
            streamWriter.Write(streamReader.ReadToEnd( ));
         }
   }
}

To replace one document part with another document part and then add contents to the updated part

  1. First, pass in parameters representing the path to and name of the source Word 2007 document and the name and path of a file containing theme content.

  2. Then, open the document as a WordprocessingDocument object.

  3. Next, delete the existing theme part from the package and then add an empty theme part.

  4. Finally, you read the content of the original theme file and write it to the new part by using a StreamReader object and a StreamWriter object.

    NoteNote

    When you delete the theme1.xml part (or most other parts), the theme1.xml.rels relationship part is also removed. Without this file, any document parts in the theme folder, such as .jpg image files, lose their relationships and are no longer available in the document. When you replace a document part, you want to add the relationship parts and modify the relationship parts to point to the new document parts. An alternative is to replace the content, rather than deleting and then recreating it.