• Sign In
 
  • MSDN Blogs
  • Microsoft Blog Images
  • More ...
Search
  • Advanced search options...
Tags
  • .NET
  • Altova
  • blogging
  • code samples
  • Codeplex
  • Custom XML
  • DII
  • DIS29500
  • ECMA-376
  • IBM
  • Java
  • Monarch
  • ODF
  • Office 2007
  • OpenXMLDeveloper.org
  • PHP
  • Redmond
  • SharePoint
  • System.IO.Packaging
  • TechEd
  • UOF
  • VSTO
  • Windows
  • WordprocessingML
  • workshops
Archives
Archives
  • January 2012 (1)
  • October 2011 (1)
  • July 2011 (2)
  • April 2011 (1)
  • March 2011 (3)
  • December 2010 (1)
  • August 2010 (1)
  • June 2010 (1)
  • May 2010 (1)
  • April 2010 (3)
  • March 2010 (1)
  • November 2009 (4)
  • October 2009 (1)
  • September 2009 (2)
  • July 2009 (2)
  • June 2009 (4)
  • May 2009 (5)
  • April 2009 (4)
  • March 2009 (4)
  • February 2009 (2)
  • January 2009 (4)
  • December 2008 (4)
  • November 2008 (3)
  • October 2008 (4)
  • September 2008 (3)
  • August 2008 (2)
  • July 2008 (5)
  • June 2008 (7)
  • May 2008 (5)
  • April 2008 (8)
  • March 2008 (14)
  • February 2008 (15)
  • January 2008 (13)
  • December 2007 (12)
  • November 2007 (5)
  • October 2007 (9)
  • September 2007 (6)
  • August 2007 (10)
  • July 2007 (9)
  • June 2007 (8)
  • May 2007 (12)
  • April 2007 (14)
  • March 2007 (12)
  • February 2007 (10)
  • January 2007 (17)
  • December 2006 (14)
  • November 2006 (10)
  • October 2006 (11)
  • September 2006 (12)
  • August 2006 (12)
  • July 2006 (12)
  • June 2006 (23)
  • May 2006 (14)
Common Tasks
  • Blog Home
  • Email Blog Author
  • About
  • RSS for comments
  • RSS for posts

CreateDOCX Sample Program

Doug Mahugh - Office Interoperability
MSDN Blogs > Doug Mahugh > CreateDOCX Sample Program

CreateDOCX Sample Program

Doug Mahugh
27 Jun 2006 5:51 PM
  • Comments 11

This post covers a very simple program for creating an Office Open XML word-processing document. The source code for this program is included in the attachment, or you can download it here.

The syntax for using the CreateDOCX program is shown to the right. It's a command-line program that takes two arguments: a filename to be created, and some text to put in the file. That's all there is to it -- the program then creates the output file using the .NET packaging API. The resulting document can be opened with Microsoft Office, but Office is not required in order to create the document.

Let's look at what's going on under the hood. The first thing to note is the namespace used for the document markup:

string WordprocessingML = "http://schemas.openxmlformats.org/wordprocessingml/2006/3/main";

This is the namespace for the WordprocessingML markup that is used in the main body part ("start part") of the document. Next, we create an XML document with the structure of a WordprocessingML document (document, body, paragraph, run, etc.), and the text (the BodyText variable) embedded inside it:

// create the start part, set up the nested structure ...
XmlDocument xmlStartPart = new XmlDocument();
XmlElement tagDocument = xmlStartPart.CreateElement( "w:document" , WordprocessingML );
xmlStartPart.AppendChild( tagDocument );
XmlElement tagBody = xmlStartPart.CreateElement( "w:body" , WordprocessingML );
tagDocument.AppendChild( tagBody );
XmlElement tagParagraph = xmlStartPart.CreateElement( "w:p" , WordprocessingML );
tagBody.AppendChild( tagParagraph );
XmlElement tagRun = xmlStartPart.CreateElement( "w:r" , WordprocessingML );
tagParagraph.AppendChild( tagRun );
XmlElement tagText = xmlStartPart.CreateElement( "w:t" , WordprocessingML );
tagRun.AppendChild( tagText );

// insert text into the start part, as a "Text" node ...
XmlNode nodeText = xmlStartPart.CreateNode(XmlNodeType.Text, "w:t", WordprocessingML);
nodeText.Value = BodyText;
tagText.AppendChild(nodeText);

Now that we've created the XML "start part" for the document, we just need to create a package (with the packaging API), add the start part to it, and create a relationship. (All parts must have a relationship.)

// create a new package (Open XML document) ...
Package pkgOutputDoc = null;
pkgOutputDoc = Package.Open( fileName , FileMode.Create , FileAccess.ReadWrite );

// save the main document part (document.xml) ...
Uri uri = new Uri( "/word/document.xml" , UriKind.Relative );
PackagePart partDocumentXML = pkgOutputDoc.CreatePart( uri,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" );
StreamWriter streamStartPart = new StreamWriter( partDocumentXML.GetStream( FileMode.Create , FileAccess.Write ) );
xmlStartPart.Save( streamStartPart );
streamStartPart.Close();
pkgOutputDoc.Flush();

// create the relationship part, close the document ...
pkgOutputDoc.CreateRelationship(uri, TargetMode.Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId1");
pkgOutputDoc.Flush();
pkgOutputDoc.Close();

That's all there is to it -- this code will generate a DOCX that loads in Word 2007 or any other valid Open XML consumer. You can copy the code from the SaveDOCX method of the attached solution, and use that as a starting point for a document-creation or document-assembly solution. Note that there is no need for the Office clients in this scenario: your code can create the DOCX without any of the complexity and scalability issues of automating Word, through use of the .NET packaging API.

The next post in this series will cover CreateXLSX, the SpreadsheetML version of this sample.

Attachment: CreateDOCX.zip
  • 11 Comments
System.IO.Packaging, code samples
Comments
  • Brian Jones: Open XML Formats
    29 Jun 2006 4:16 PM
    Doug Mahugh had a post a couple days ago where he provides some basic code that will generate a WordprocessingML...
  • Doug Mahugh
    15 Jul 2006 7:09 PM
    This post covers the code for a CreateXlsx program that creates a simple Open XML spreadsheet from scratch...
  • OpenXML Developer
    20 Jul 2006 6:08 PM
    Creating Open XML documents from a template can help simplify your work and allow for changes to the...
  • Matt W's Windows Workflow Place
    29 Aug 2006 7:07 PM
    One of the nice things about the WF activity model is that you can pretty rapidly take existing code...
  • All About Interop
    3 Oct 2006 4:07 PM

    A while back, the OpenXmlDeveloper.org website offered an example of how to create a WordProcessingML

  • Doug Mahugh
    5 Dec 2006 3:00 AM

    Here at the Open XML workshop in Paris this week, Jerome Berthaud pointed out to me that my CreateXlsx

  • We did start the fire (sorry Billy)
    6 Dec 2006 10:10 AM

    Ich hatte versprochen, die Ressourcen für meine beiden Vorträge auf der Office, Vista und Exchange Launch

  • Dating
    31 May 2008 4:54 AM

    This post covers a very simple program for creating an Office Open XML word-processing document. The source code for this program is included in the attachment, or you can download it here . The syntax for using the CreateDOCX program is shown to the

  • Weddings
    6 Jun 2008 8:38 AM

    This post covers a very simple program for creating an Office Open XML word-processing document. The source code for this program is included in the attachment, or you can download it here . The syntax for using the CreateDOCX program is shown to the

  • Doug Mahugh CreateDOCX Sample Program | Outdoor Ceiling Fans
    2 Jun 2009 1:59 PM

    PingBack from http://outdoorceilingfansite.info/story.php?id=56519

  • Doug Mahugh CreateDOCX Sample Program | Quick Diets
    9 Jun 2009 6:49 AM

    PingBack from http://quickdietsite.info/story.php?id=9418

Page 1 of 1 (11 items)
  • © 2012 Microsoft Corporation.
  • Terms of Use
  • Trademarks
  • Privacy Statement
  • Report Abuse
  • 5.6.402.223