none
Unable to open .docx created with OpenXML SDK 2.5 in Word 2007 RRS feed

  • Question

  • Hi,

    I use OpenXML SDK 2.5 to open and modify a .dotx template in c# and save it in .docx format. Most of our machines are able to open the resulting file with no difficulty, but I've discovered that those of us using Word 2007 (not 2010 or 2013) get an error message when they try to open the .docx file: "The file ____.docx cannot be opened because there are problems with the contents." The details further specify: "The file is corrupt and cannot be opened. Location: Part: /word/settings.xml, Line: 1, Column: 2668"

    Below is a portion of my code (the part responsible for creating the .docx file):

    File.Copy(sourceFile, destinationFile, true);
    
    using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true)){ 
      // Change the document type to Document 
      Documentdocument.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
       // Get the MainPart of the document
       MainDocumentPart mainPart = document.MainDocumentPart;
       // Get the Document Settings Part
       DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart;
       // Create a new attachedTemplate and specify a relationship ID
       AttachedTemplate attachedTemplate1 = new AttachedTemplate() { Id = "relationId1" };
       // Append the attached template to the DocumentSettingsPart
       documentSettingPart1.Settings.Append(attachedTemplate1);
       // Add an ExternalRelationShip of type AttachedTemplate.
       // Specify the path of template and the relationship
       IDdocumentSettingPart1.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(sourceFile, UriKind.Absolute), "relationId1");
    
       // Get a list of bookmarks
       IDictionary<String, BookmarkStart> bookmarkMap = new Dictionary<String, BookmarkStart>();
       foreach (BookmarkStart bookmarkStart in mainPart.RootElement.Descendants<BookmarkStart>())
       {
          bookmarkMap[bookmarkStart.Name] = bookmarkStart;
       }
       // Make changes to bookmarks
       foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
       {
           if (bookmarkStart.Name == "WorkOrderNo")
           {
                Text text = new Text(WorkOrderNum);
                FontSize WOFontSize = new FontSize();
                WOFontSize.Val = "28";
                Run run = new Run(new RunProperties(new Bold(), WOFontSize));
                run.Append(text);
                bookmarkStart.InsertAfterSelf(run);
            }
            else if (bookmarkStart.Name == "BillingCode")
            {
                 Text text = new Text(BillingCode);
                 FontSize WOFontSize = new FontSize();
                 WOFontSize.Val = "28";
                 Run run = new Run(new RunProperties(new Bold(), WOFontSize));
                 run.Append(text);
                 bookmarkStart.InsertAfterSelf(run);
             }
       }
       // Save the document
       mainPart.Document.Save();}

    Word 2007 users are able to open the file if they choose "OK" on the prompt, and then "Yes" on the prompt reading: "Word found unreadable content in _____.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes." While this is an acceptable short-term workaround, I'm hoping to come up with a solution to the problem. Any ideas?

    (I can send/attach the .dotx and .docx files if desired; I just couldn't figure out how to attach them to this post)

    Thanks,

    - Jim


    • Edited by Jim Heide Thursday, January 30, 2014 5:44 PM typo
    Thursday, January 30, 2014 5:10 PM

All replies

  • Hi Jim,

    I failed to run the code above. According to your description, you can open the docx file generate by Open XML SDK 2.5 with Word 2010 and Word 2013 but it failed in Word 2007. Did I understand correctly?

    >> The details further specify: "The file is corrupt and cannot be opened. Location: Part: /word/settings.xml, Line: 1, Column: 2668"<<

    According to the error message, I suggest you check the setting.xml via Open XML SDK 2.5 productivity Tool. I create a blank word and open it with the tools and the setting.xml like below:

    <w:settings 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: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:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14 w15">
    
      <w:zoom w:percent="100" />
    
      <w:doNotDisplayPageBoundaries />
    
      <w:defaultTabStop w:val="720" />
    
      <w:characterSpacingControl w:val="doNotCompress" />
    
      <w:compat>
    
        <w:useFELayout />
    
        <w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="15" />
    
        <w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
    
        <w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
    
        <w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
    
        <w:compatSetting w:name="differentiateMultirowTableHeaders" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
    
      </w:compat>
    
      <w:rsids>
    
        <w:rsidRoot w:val="00D61673" />
    
        <w:rsid w:val="000D1EF3" />
    
        <w:rsid w:val="00154B84" />
    
        <w:rsid w:val="00247849" />
    
        <w:rsid w:val="002647DE" />
    
        <w:rsid w:val="00401892" />
    
        <w:rsid w:val="00424E7F" />
    
        <w:rsid w:val="004647BD" />
    
        <w:rsid w:val="0049039B" />
    
        <w:rsid w:val="004A53B4" />
    
        <w:rsid w:val="004E0A38" />
    
        <w:rsid w:val="00624967" />
    
        <w:rsid w:val="00630F82" />
    
        <w:rsid w:val="006862BF" />
    
        <w:rsid w:val="006E6CE6" />
    
        <w:rsid w:val="006F5810" />
    
        <w:rsid w:val="00814D4A" />
    
        <w:rsid w:val="00961B2F" />
    
        <w:rsid w:val="009B56D5" />
    
        <w:rsid w:val="009C73A7" />
    
        <w:rsid w:val="00A95357" />
    
        <w:rsid w:val="00B26944" />
    
        <w:rsid w:val="00B64BEB" />
    
        <w:rsid w:val="00BF1C51" />
    
        <w:rsid w:val="00C21AD0" />
    
        <w:rsid w:val="00C7061A" />
    
        <w:rsid w:val="00D61673" />
    
        <w:rsid w:val="00D93CFE" />
    
        <w:rsid w:val="00DE1D60" />
    
        <w:rsid w:val="00E27B55" />
    
        <w:rsid w:val="00E31447" />
    
        <w:rsid w:val="00EF5D7E" />
    
        <w:rsid w:val="00FB3D63" />
    
        <w:rsid w:val="00FC79BB" />
    
      </w:rsids>
    
      <m:mathPr>
    
        <m:mathFont m:val="Cambria Math" />
    
        <m:brkBin m:val="before" />
    
        <m:brkBinSub m:val="--" />
    
        <m:smallFrac m:val="0" />
    
        <m:dispDef />
    
        <m:lMargin m:val="0" />
    
        <m:rMargin m:val="0" />
    
        <m:defJc m:val="centerGroup" />
    
        <m:wrapIndent m:val="1440" />
    
        <m:intLim m:val="subSup" />
    
        <m:naryLim m:val="undOvr" />
    
      </m:mathPr>
    
      <w:themeFontLang w:val="en-US" w:eastAsia="zh-CN" />
    
      <w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
    
      <w:shapeDefaults>
    
        <o:shapedefaults v:ext="edit" spidmax="1026" />
    
        <o:shapelayout v:ext="edit">
    
          <o:idmap v:ext="edit" data="1" />
    
        </o:shapelayout>
    
      </w:shapeDefaults>
    
      <w:decimalSymbol w:val="." />
    
      <w:listSeparator w:val="," />
    
      <w15:chartTrackingRefBased />
    
      <w15:docId w15:val="{C49C27DE-50F7-4BDB-A5A4-EB6D84F13A6E}" />
    
    </w:settings>
    

    You can check the difference between the two files.

    Hope it is helpful, if the issue still exists, would you mind up upload a workable sample to help us to reproduce this issue?

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 31, 2014 6:44 AM
    Moderator
  • Your understanding of the situation is correct -- the primary difference between the .docx file before and after someone with Word 2007 opened and saved over it is the removal of the following line from settings.xml:

        <w:attachedTemplate r:id="relationId1" />

    Although the following two lines are also added after saving:

        <w:rsid w:val="00E6220F" />

        <w:rsid w:val="00F30545" />

    Below are two files I used (renamed as .gifs, since I still can't figure out how to attach non-image files -- I apologize for the inconvenience):

    Class file (rename as .cs):


    Template file (rename as .dotx):


    I tried using the code suggested by the Productivity Tool to address the difference:

    Settings settings1 = documentSettingsPart1.Settings;
    
    Rsids rsids1=settings1.GetFirstChild<Rsids>();
    AttachedTemplate attachedTemplate1=settings1.GetFirstChild<AttachedTemplate>();
    
    Rsid rsid1=rsids1.Elements<Rsid>().ElementAt(6);
    Rsid rsid2=rsids1.Elements<Rsid>().ElementAt(25);
    
    Rsid rsid3 = new Rsid(){ Val = "002E2FFC" };
    rsids1.InsertBefore(rsid3,rsid1);
    
    Rsid rsid4 = new Rsid(){ Val = "00F67758" };
    rsids1.InsertBefore(rsid4,rsid2);
    
    attachedTemplate1.Remove();

    ...but for some reason, the differences persist.

    Do you have any suggestions about how I might alter my code to account for this? (I can send you the class I'm using to generate the word documents if you'd like, though it looks much like the code in the first post)

    Thanks,

    - Jim

    Monday, February 3, 2014 3:06 PM