How to: Duplicate an Existing Document Part from 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 copy a duplicate part (file) from one Office Open XML package to another package 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.

Duplicate an Existing Document Part from an Office Open XML Package

In the following code, you duplicate an existing document part of a WordprocessingDocument package:

    ' How to duplicate an existing package part
    Public Sub CopyTheme(ByVal fromDocument1 As String, ByVal toDocument2 As String)
        Dim wordDoc1 As WordprocessingDocument = WordprocessingDocument.Open(fromDocument1, False)
        Dim wordDoc2 As WordprocessingDocument = WordprocessingDocument.Open(toDocument2, True)
        Dim themePart1 As ThemePart = wordDoc1.MainDocumentPart.ThemePart
        wordDoc2.MainDocumentPart.AddPart(themePart1)
    End Sub
//  How to duplicate an existing package part.
public void CopyTheme(string fromDocument1, string toDocument2) 
{
    WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1, false);
    WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true);
    ThemePart themePart1 = wordDoc1.MainDocumentPart.ThemePart;
    wordDoc2.MainDocumentPart.AddPart(themePart1);
}

To duplicate an existing document part of a WordprocessingDocument package

  1. First, you pass in parameters representing the path to and the name of the Word 2007 source file and target file.

  2. Then, specify the package part ID that you want to duplicate.

  3. Next, open the document as a WordprocessingDocument object.

  4. Then, create a reference to the document part that you want to duplicate. In this case you duplicate a ThemePart part of the document.

  5. Finally, you use the AddPart method to copy the ThemePart part to the MainDocumentPart part of the document.