Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am generating word documents via the openxml sdk for office, and can't figure out why my documents open in compatibility mode. Often times they crash word when I try to close them. The packages pass the validation as it is checked by the productivity tool that you can also get from MSDN.

The following very basic code creates a document that will open in compatibility mode and seems to crash word for me quite often.
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document))
{
  MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
  mainPart.Document = new Document();
  mainPart.Document.Append(new Body());
  mainPart.Document.Body.Append(new Paragraph(new Run(new Text("Test"))));
}

I seem to be missing something pretty fundamental, but all the examples I find are doing the same thing. Does anyone know what is the problem?

thanks,

Danny


UPDATE from OP:
Solved by OP himself. Posted as one of the answers.
Posted
Updated 30-Mar-11 22:44pm
v4
Comments
GenJerDan 30-Mar-11 23:38pm    
Are you doing SetPackageProperties(document); at the end of the CreatePackage function? With the SetPackageProperties function creating it an an OpenXMLPackage?
i.e.
SetPackageProperies(OpenXmlPackage document)

Just a little bit late, but I wouldn't do it with an external dependency like an XML-File.

My suggestion is:
C#
private static void AddSettingsToMainDocumentPart(MainDocumentPart part) {
    DocumentSettingsPart settingsPart = part.AddNewPart<documentsettingspart>();
    settingsPart.Settings = new Settings(
       new Compatibility(
           new CompatibilitySetting() { 
               Name = new EnumValue<compatsettingnamevalues>(CompatSettingNameValues.CompatibilityMode),
               Val = new StringValue("14"),
               Uri = new StringValue("http://schemas.microsoft.com/office/word")
           }
       )
    );
    settingsPart.Settings.Save();
}</compatsettingnamevalues></documentsettingspart>


This will add compatibilty to Office 2010 (referring to the magic "14") for example.
 
Share this answer
 
Ok I figured this out, and it turned out to be similar to what I had to do for styles in some ways.

The answer someone posted on this forum post got me started:
http://social.msdn.microsoft.com/Forums/is/oxmlsdk/thread/7d55514b-f846-4790-b545-3693ab18d966[^]

I took the xml in his answer and made it into an xml file in my project. I then set that to copy always in its properties. After doing that, I use the following function to add the settings to the document, and it will open correctly for Word 2010:

private static void AddSettingsToMainDocumentPart(MainDocumentPart part)
      {
          DocumentSettingsPart settingsPart = part.AddNewPart<DocumentSettingsPart>();
          FileStream settingsTemplate = new FileStream("settings.xml", FileMode.Open, FileAccess.Read);
          settingsPart.FeedData(settingsTemplate);
          settingsPart.Settings.Save();
      }


Doing that has made it so my documents open without being in compatibility mode.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900