none
AltChunk in table, Docx validates, Word won't read it RRS feed

  • Question

  • Open XML SDK 2.0 most recent version
    Word 2007 on Win7
    C# 2008 Express

    My program can set properties, can replace Content Controls in the body with AltChunks, and other stuff.

    I am using SDK validation per http://blogs.msdn.com/brian_jones/archive/2009/09/24/finding-open-xml-errors-with-open-xml-sdk-validation.aspx

    When I attempt to replace a Content Control in a table it fails as follow:
    - Validation in my code says there are no errors
    - Validation in the SDK Tool says there are no errors
    - Word 2007 says the file cannot be opened because there are problems with the contents.  Unspecified error.  It does not give me the chance to load it anyway.

    Looking at the output file with both the SDK Tool and at the actual XML with Vis Studio: 
    - XML appears to be well-formed in that the AltChunk is a sibling to another paragraph in the cell and exactly replaced the Control.
    - The Word Details Location is the <w:tc> just after the </w:tc> following the altChunk

    Is there some known problem with tables?

    Suggestions on what to try next?

    Thanks for any help.
    Sunday, January 17, 2010 1:30 AM

All replies

  • More information.

    Editing the source document with Word:

    This fails as described above:  Control being replaced is at the end of cell with no included paragraph mark

    This works:  Control being replaced includes an extra paragraph mark before the end of cell

    Unfortunately, this places an extra line at the end of the cell and makes it larger vertically than I want.

    Thanks for any help
    Sunday, January 17, 2010 4:30 AM
  • More information.

    I simplified the test docx down to a single table.  Then I ran the test with and without the extra paragraph mark and compared the differences between the two files using the SDK Tool.  The only significant (i.e. other than times, counts, rsId) difference was the addition of this after the altChunk:

              <w:p w:rsidR="00373078" w:rsidRDefault="00373078" />

    So, then I pasted the extra item into the bad file, rezipped and it worked fine with Word.

    I have included the complete formatted document.xml from the good and bad cases below.

    Is this a known Word problem?  Or am I still doing something wrong?

    Any help would be greatly appreciated. 


    GOOD FILE document.xml:  Word 2007 accepts this

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <w:document xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:body>
        <w:p w:rsidR="009957B0" w:rsidRDefault="009957B0" />
        <w:tbl>
          <w:tblPr>
            <w:tblStyle w:val="TableGrid" />
            <w:tblW w:w="0" w:type="auto" />
            <w:tblLook w:val="04A0" />
          </w:tblPr>
          <w:tblGrid>
            <w:gridCol w:w="4788" />
            <w:gridCol w:w="4788" />
          </w:tblGrid>
          <w:tr w:rsidR="00C04B82" w:rsidTr="00C04B82">
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
            </w:tc>
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82">
                <w:r>
                  <w:t>Test:</w:t>
                </w:r>
              </w:p>
              <w:altChunk r:id="AltChunkId1" />
              <w:p w:rsidR="00373078" w:rsidRDefault="00373078" />
            </w:tc>
          </w:tr>
          <w:tr w:rsidR="00C04B82" w:rsidTr="00C04B82">
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
            </w:tc>
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
            </w:tc>
          </w:tr>
        </w:tbl>
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:sectPr w:rsidR="00C04B82" w:rsidSect="009957B0">
          <w:pgSz w:w="12240" w:h="15840" />
          <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
          <w:cols w:space="720" />
          <w:docGrid w:linePitch="360" />
        </w:sectPr>
      </w:body>
    </w:document>


    BAD FILE document.xml -- Word 2007 won't accept but validates OK with SDK 2

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <w:document xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:body>
        <w:p w:rsidR="009957B0" w:rsidRDefault="009957B0" />
        <w:tbl>
          <w:tblPr>
            <w:tblStyle w:val="TableGrid" />
            <w:tblW w:w="0" w:type="auto" />
            <w:tblLook w:val="04A0" />
          </w:tblPr>
          <w:tblGrid>
            <w:gridCol w:w="4788" />
            <w:gridCol w:w="4788" />
          </w:tblGrid>
          <w:tr w:rsidR="00C04B82" w:rsidTr="00C04B82">
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
            </w:tc>
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82">
                <w:r>
                  <w:t>Test:</w:t>
                </w:r>
              </w:p>
              <w:altChunk r:id="AltChunkId1" />
            </w:tc>
          </w:tr>
          <w:tr w:rsidR="00C04B82" w:rsidTr="00C04B82">
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
            </w:tc>
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="4788" w:type="dxa" />
              </w:tcPr>
              <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
            </w:tc>
          </w:tr>
        </w:tbl>
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:p w:rsidR="00C04B82" w:rsidRDefault="00C04B82" />
        <w:sectPr w:rsidR="00C04B82" w:rsidSect="009957B0">
          <w:pgSz w:w="12240" w:h="15840" />
          <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
          <w:cols w:space="720" />
          <w:docGrid w:linePitch="360" />
        </w:sectPr>
      </w:body>
    </w:document>
    Sunday, January 17, 2010 7:22 PM
  • I guess nobody knows the answer here.

    Where do I go to get this type of thing answered?
    Thursday, January 21, 2010 10:24 PM
  • Hi airp,

    Thanks for your question.

    We have checked your document.xml and found that it is not due to lack of "<w:p w:rsidR="00373078" w:rsidRDefault="00373078" />" that you can't open it in Word. To investigate your case in detail, could you please show us the content of your BAD file's ".rels" and "Content_Types.xml"?

    Thanks,

    Lu
    Tuesday, January 26, 2010 1:41 AM
  • I recently ran into this same issue.  Documentation indicates that altChunk's parent can be w:tc .  This is fine except it doe not work if w:altChunk is last element in w:tc.  Word will not open the file, it will indicate there is an unspecified error.

    I would assume this is probably bad implementation of the spec/bug in Word it self.

    This will not work:
    w:tc
       w:p
       w:altChunk

    Ant this will work:
    w:tc
       w:p
       w:altChunk
       w:p

    According to documentation both are correct, but first one will make an .docx that Word cannot open.

    My temporary workaround is to insert that extra paragraph and make it font size 1, but for what I am trying to implement, I cannot expect my users to use that "workaround".
    Tuesday, February 2, 2010 7:15 PM
  • vj9999 is correct. There is an implementation bug within Word such that having a cell with an altChunk with no paragraph after results in Word complaining. The proposed workaround of adding a black paragraph after the altchunk should work.

    Thanks for reporting this issue.
    Zeyad Rajabi (MS)
    • Proposed as answer by vj9999 Wednesday, February 3, 2010 12:08 AM
    Tuesday, February 2, 2010 9:55 PM
  • Thanks Zeyad
    Wednesday, February 3, 2010 12:08 AM
  • Hi I'm wondering what the code is that you are using to target the Alt Chunk between the two paragraphs in the table cell. 

    body.InsertAfter(altChunk, tablecell.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last()); for instance? (not that this is working for me)
    Wednesday, February 3, 2010 5:45 PM
  • Hi everyone. Propably too late now to help anyone, but maybe in the future it might help someone. I wrote a Method to prepare a document for HTML insertion by looking at Bookmarks.

        private static XmlDocument prepareHTMLChunks(PackagePart partDocumentXML)
        {
          XmlDocument _document = new XmlDocument();
          Stream xmlStream = partDocumentXML.GetStream();
          _document.Load(xmlStream);
    
          XmlNamespaceManager _ns = new XmlNamespaceManager(_document.NameTable);
          
          _ns.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    
          XmlNode _body = _document.SelectSingleNode("//w:body", _ns);
    
          foreach (XmlNode _node in _document.SelectNodes("//w:bookmarkStart", _ns))
          {
            foreach (XmlAttribute _attribute in _node.Attributes)
            {
              if (_attribute.Value.EndsWith("HTML"))
              {
    
                XmlElement _element = _document.CreateElement("w", "altChunk", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                XmlAttribute _newAttr = _document.CreateAttribute("r", "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
                _newAttr.Value = _attribute.Value;
                _element.Attributes.Append(_newAttr);
    
                XmlNode nodeToreplace = _node.ParentNode;
    
                if (nodeToreplace.ParentNode.Name != _body.Name)
                {
                  //THE BOOKMARK IS INSIDE A TABLE. WE NOW HAVE TO FIND THE CORRECT PARENT OF BOOKMARK
                  XmlNode _bookmarkParent = _node.ParentNode;
                  XmlNode _bookmarkParentParent = _bookmarkParent.ParentNode;
                  _bookmarkParentParent.InsertBefore(_element,_bookmarkParent);
                }
                else
                {
                  //NORMAL BOOKMARK DIRECTLY IN THE BODY
                  _body.InsertAfter(_element, nodeToreplace);            
                }
              }      
            }
          }      
          return _document;    
        }
    

     

    Monday, September 6, 2010 10:06 AM