Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Building Word 2007 Documents Using Office Open XML Formats

Office 2007

Office Visual How To

Erika Ehrli, Brian Jones, Microsoft Corporation

Applies to: 2007 Microsoft Office System, Microsoft Office Word 2007, Office Open XML Formats

Overview

The Office Open XML Formats are based on XML and ZIP archive technologies. The new file format in Microsoft Office Word 2007 divides the file into document parts, each of which defines a part of the overall contents of the file. You can easily create, change, add, or delete data in a Word 2007 file programmatically or manually.

Code It

To illustrate how document parts, content type items, and relationship items work together, this section walks through the process of building a Word XML format document in Word 2007.

To create a Word 2007 document that contains content type and relationship items, you need to create a root folder that contains a specific folder and file structure, as shown in Figure 1.

Figure 1. Folder and file structure for a Word 2007 document

Folder and file structure for a Word 2007 document

After you create all folders and files, the next section walks you through adding the required XML code to each document part.

Creating the Document Properties

First, you need to create two XML files for the document properties:

  1. Create a folder and name it root.

  2. Create a folder inside the folder root and name it docProps.

  3. Open Notepad or any other XML editor.

  4. Copy the following code into a new file and save it as app.xml inside the docProps folder.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <Properties xmlns=
       "http://schemas.openxmlformats.org/officeDocument/2006/
       extended-properties"xmlns:vt="http://schemas
       .openxmlformats.org/officeDocument/2006/
       docPropsVTypes"> 
        <Template>Normal.dotm</Template>
        <TotalTime>1</TotalTime> 
        <Pages>1</Pages> 
        <Words>3</Words> 
        <Characters>23</Characters> 
        <Application>Microsoft Office Word</Application> 
        <DocSecurity>0</DocSecurity> 
        <Lines>1</Lines> 
        <Paragraphs>1</Paragraphs> 
        <ScaleCrop>false</ScaleCrop> 
        <Company>MS</Company> 
        <LinksUpToDate>false</LinksUpToDate> 
        <CharactersWithSpaces>25</CharactersWithSpaces> 
        <SharedDoc>false</SharedDoc> 
        <HyperlinksChanged>false</HyperlinksChanged> 
        <AppVersion>12.0000</AppVersion> 
    </Properties> 
    
    
  5. Open Notepad or any other XML editor.

  6. Copy the following code into a new file and save it as core.xml inside the docProps folder.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <cp:coreProperties xmlns:cp=
       "http://schemas.openxmlformats.org/package/2006/metadata/
       core-properties"xmlns:dc="http://purl.org/dc/
       elements/1.1/"xmlns:dcterms="http://purl.org/
       dc/terms/"xmlns:dcmitype="http://purl.org/
       dc/dcmitype/"xmlns:xsi="http://www.w3.org/
       2001/XMLSchema-instance"> 
       <dc:title></dc:title> 
       <dc:subject></dc:subject> 
       <dc:creator>Your name</dc:creator> 
       <cp:keywords></cp:keywords> 
       <dc:description></dc:description> 
       <cp:lastModifiedBy>Your name</cp:lastModifiedBy> 
       <cp:revision>2</cp:revision> 
       <dcterms:created xsi:type="dcterms:W3CDTF">
          2006-05-03T01:13:00Z</dcterms:created> 
       <dcterms:modified xsi:type="dcterms:W3CDTF">
          2006-05-03T01:14:00Z</dcterms:modified> 
    </cp:coreProperties> 
    
    

Creating the Document

Next, you need to create an XML file for the document part. This is the only required part in the new Word XML format.

  1. Create a folder and name it root.

  2. Create a folder inside the root folder and name it word.

  3. Open Notepad or any other XML editor.

  4. Copy the following code into a new file and save it as document.xml inside the word folder.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <w:document 
       xmlns:ve="http://schemas.openxmlformats.org/
       markup-compatibility/2006" xmlns:o=
       "urn:schemas-microsoft-com:office:
       office" xmlns:o12="http://schemas.microsoft.com
       /office/2004/7/core" xmlns:r=
       "http://schemas.openxmlformats.org/
       officeDocument/2006/relationships" xmlns:
       m="http://schemas.microsoft.com/office/omml/
       2004/12/core" xmlns:v="urn:schemas-microsoft-com:
       vml" xmlns:wp="http://schemas.openxmlformats.
       org/drawingml/2006/3/wordprocessingDrawing" 
       xmlns:w10="urn:schemas-microsoft-com:
       office:word" xmlns:w="http://schemas.openxml
       formats.org/wordprocessingml/2006/3/main"> 
       <w:body> 
        <w:p> 
          <w:r w:rsidR="002847EC"> 
          <w:t>Word 2007 rocks my world!</w:t> 
          </w:r> 
        </w:p> 
       </w:body> 
    </w:document> 
    
    

Creating a Relationship

Next, you need to create a relationship to this part. This relationship is documented in the root _relsfolder, which means that the relationship is off the root (or start part) of the package. To create the relationship:

  1. Create a folder inside the folder root and name it _rels.

  2. Open Notepad or any other XML editor.

  3. Copy the following code into a new file and save it as .relsinside the _rels folder.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <Relationships xmlns="http://schemas.openxmlformats.
    org/package/2006/relationships"> 
      <Relationship Id="rId3" Type=
       "http://schemas.openxmlformats.org/officeDocument/
          2006/relationships/extended-properties" 
       Target="docProps/app.xml"/> 
       <Relationship Id="rId2" Type=
       "http://schemas.openxmlformats.org/package/2006/
          relationships/metadata/core-properties" 
       Target="docProps/core.xml"/> 
       <Relationship Id="rId1" Type=
       "http://schemas.openxmlformats.org/officeDocument/
          2006/relationships/officeDocument" 
       Target="word/document.xml"/> 
    </Relationships> 
    
    
  4. Notice that this XML creates a relationship of type officeDocument with ID rId1 to the document.xml file in the folder named word.

Defining the Content Type

Next, you need to define the content type of this file.

  1. Note that the structure of a content type definition file looks like the following code.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <Types xmlns="http://schemas.openxmlformats.org/
    package/2006/content-types"> 
       <Default Extension="rels" ContentType=
       "application/vnd.openxmlformats-package.
       relationships+xml"/> 
       <Default Extension="xml" ContentType=
       "application/xml"/> 
       <Override PartName="/word/document.xml" 
    ContentType="application/
          vnd.openxmlformats-officedocument.
          wordprocessingml.document.main+xml"/> 
       <Override PartName="/word/styles.xml" ContentType=
       "application/vnd.openxmlformats-officedocument.
       wordprocessingml.styles+xml"/> 
       <Override PartName="/docProps/app.xml" 
       ContentType="application/vnd.openxmlformats
       -officedocument.extended-properties+xml"/> 
       <Override PartName="/word/settings.xml" 
       ContentType="application/vnd.openxmlformats
       -officedocument.wordprocessingml.settings
       +xml"/> 
       <Override PartName="/word/theme/theme1.xml" ContentType=
       "application/vnd.openxmlformats-officedocument.
       theme+xml"/> 
       <Override PartName="/word/fontTable.xml" 
       ContentType="application/vnd.openxmlformats
       -officedocument.wordprocessingml.fontTable
       +xml"/> 
       <Override PartName="/word/webSettings.xml" ContentType=
       "application/vnd.openxmlformats-officedocument.
       wordprocessingml.webSettings+xml"/> 
       <Override PartName="/docProps/core.xml" 
       ContentType="application/vnd.openxmlformats
       -package.core-properties+xml"/> 
    </Types> 
    
    
  2. Open Notepad or any other XML editor.

  3. Copy the above code into a new file and save it as [Content_Types].xml inside the root folder.

    NoteNote
    This reserved file name is used by the Open Packaging Conventions to define the content types of all files in the package.

Creating the Package

Finally, you can put these files into a ZIP package to create a valid Word 2007 document:

  1. Using any ZIP utility, save all the content of the simpledocument folder into a ZIP archive, including the following subfolders: the docProps folder, the word folder, and the _rels folder. Also include [Content_Types].xml.

    Important noteImportant
    Do not simply add the complete simpledocument folder to a ZIP file or you get an internal error while opening the file in Word 2007. You need to specifically add all the subfolders of the simpledocument folder to the ZIP archive.
  2. Save the archive as simpledocument.docx.

Now, you can open this file in Word 2007 and see the contents of the package.

Read It

The file format in Word 2007 consists of a compressed ZIP file, called a package. This package holds all of the content that is contained within the document. You can extract and open the files in the package to reveal component parts that give you access to the structures that compose the file. Figure 2 shows the file structure of a sample Word 2007 document.

Figure 2. Hierarchical file structure of a typical Word 2007 document

Hierarchical file structure of a typical Word 2007

To understand the structure of a Word 2007 document, you must understand the three major components of the new file format:

  • Part items. Each part item corresponds to one file in the un-zipped package. For example, if you right-click a Microsoft Office Excel workbook and choose to extract it, you see a workbook.xml file, several sheetn.xml files, and other files. Each of those files is a document part in the package.

  • Content Type items. Content type items describe what file types are stored in a document part. For example, image/jpeg denotes a JPEG image. This information enables Microsoft Office, and third-party tools, to determine the contents of any part in the package and to process its contents accurately.

  • Relationship items. Relationship items specify how the collection of document parts come together to form a document. This method specifies the connection between a source part and a target resource. Relationships are stored within XML parts in the document package, for example, /_rels/.rels.

You can replace and add entire document parts to change the content, properties, or formatting of Word 2007 documents. For more information about the Word 2007 file format, read the article Walkthrough: Word 2007 XML Format.

You can also build Word 2007 documents programmatically by using the Microsoft WinFX System.IO.Packaging class. For example, by using the Microsoft WinFX System.IO.Packaging class, you can create a document part with the PackagePart.CreatePart method. For more information about PackageParts, see the PackagePart Class reference documentation in the Microsoft Windows SDK.

See It

Video splash screen

Watch the Video

Video Length: 00:08:24

File Size: 9.55 MB

File Type: WMV file

Explore It
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft