1
votes

I have some code that creates a document to be viewed in MS Word. Some XML is run through XSLT, then added into the OpenXML document. The resulting document looks fine in Word 2010, but won't open in Word 2007, which I need to be able to support. How can I create a valid Word 2007 document?

With Open XML Format SDK 2.0 (version 2.0.5022.0)

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

...

using (WordprocessingDocument package = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document))
{

    package.AddMainDocumentPart();

    MainDocumentPart mainPart = package.MainDocumentPart;

    mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();

    // code ... XML translation and append to document

    package.MainDocumentPart.Document.Save();
}

.


This post has some useful information:

There are differences between Word 2010 and Word 2007, and they adhere to different versions of the OOXML standard. It is quite possible that you have included content that works in Word 2010, but that Word 2007 can't handle. You don't provide any helpful information to say what that may be, but when Word 2010 creates documents it uses "AlternateContent" tags to provide different versions of the xml for different versions of Word and you may have to do the same, or limit yourself to Word 2007-compatible content.

How would I know what "Word 2007-compatible content" is in advance?

2

2 Answers

0
votes

One of the most common causes of this symptom is that the document contains elements/attributes in the Office 14 namespace.

As an example, you might have markup like this:

        <w:rPr>
      <w:color w:val="5B9BD5"
               w:themeColor="accent1"/>
      <w14:shadow w14:blurRad="38100"
                  w14:dist="25400"
                  w14:dir="5400000"
                  w14:sx="100000"
                  w14:sy="100000"
                  w14:kx="0"
                  w14:ky="0"
                  w14:algn="ctr">
        <w14:srgbClr w14:val="6E747A">
          <w14:alpha w14:val="57000"/>
        </w14:srgbClr>
      </w14:shadow>

You probably have that w14 namespace declared, but there also needs to be an attribute in the root element:

mc:Ignorable="w14 w15 wp14"

You need to have this attribute declared:

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
        xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
        xmlns:v="urn:schemas-microsoft-com:vml"
        xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
        xmlns:w10="urn:schemas-microsoft-com:office:word"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
        xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
        xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
        xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
        xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
        xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
        xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
        mc:Ignorable="w14 w15 wp14">

This also means that you need to specifically use those namespace prefixes.

If this is not the problem, if you want, I can take a look at your document. I can probably tell you quite quickly why it won't open in 2007. If you post on the forum at OpenXMLDeveloper.org, the question/sample document will automatically come to my attention.

Cheers, Eric

0
votes

Opening my document in Word 2007 showed that the line

<w:tblW w:w="auto" w:type="pct" />

was causing problems. Using the OpenXML SDK tool, for validation, I found several errors causing validation errors:

  • Doubles being used as Int32
  • Order of elements is important (per the schema and this question)
  • w:w="auto" is not a valid Int32

Run time validation can also be performed as shown here.

Changed the w:tblPr to:

<w:tblPr>
    <w:tblW w:type="auto" />
</w:tblPr>

After fixing the validation errors, the document still wouldn't open, which led to the tblW tag:

The preferred width for the table is specified with the <w:tblW> element within the <w:tblPr> element. All widths in a table are considered preferred because widths of columns can conflict and the table layout rules can require a preference to be overridden. If this element is omitted, then the cell width is of type auto.

Removing the w:tblW element fixed the problem.

Going forward, it doesn't appear as if there is any way to validate an OpenXml such that it will be compatible with Word 2007.