Click here to Skip to main content
12,696,632 members (31,257 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
I am creating a sample document using the code below.

public static void CreateDocumentFile(string file)
        {
            // Create a Wordprocessing document. 
            using (WordprocessingDocument package = WordprocessingDocument.Create(file, WordprocessingDocumentType.Document))
            {
                // Add a new main document part. 
                package.AddMainDocumentPart();

                // Create the Document DOM. 
                package.MainDocumentPart.Document =
                  new Document(
                    new Body(
                      new Paragraph(
                        new Run(
                          new Text("Hello World!")))));
                
                // Save changes to the main document part. 
                package.MainDocumentPart.Document.Save();
            }


Document is created at the desired location but when i open it, gives me an error ,

The file cannot be opened because there are some problem in the content and it is pointing that my document.xml file contains error on line:1 col:198

my document.xml contains
<pre lang="xml"><?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:r><a:t>Hello World!</a:t></a:r></a:p></w:body></w:document>
Posted 18-Feb-13 2:05am
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 14:39pm
   
Can you open this document with Word. Are you using compatible version of Microsoft.Office.Interop?
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hello,

That's a wrong using statement...

The Paragraph class exists in 3 different namespaces, and I think you chose the wrong one.

When dealing with a Word document you should use DocumentFormat.OpenXml.Wordprocessing.Paragraph

I think you used DocumentFormat.OpenXml.Drawing.Paragraph as the schema shown in your example is the schema for the drawing namespace.

So the solution is simple in your using section, remove:

using DocumentFormat.OpenXml.Drawing;

and replace by the correct one:

using DocumentFormat.OpenXml.Wordprocessing;


Valery.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

By testing your code with Open XML Format SDK 2.0[^] (v 2.0.5022.0) [NET 3.5]
or Open XML SDK 2.5[^] (v 2.5.5631.0) [NET 4.0] my output looked like this (output formatted):
<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
 <w:body>
  <w:p>
   <w:r>
    <w:t>Hello World!</w:t>
   </w:r>
  </w:p>
 </w:body>
</w:document>

and I was able to open the Document in Word 2007 and WordPad 6.
  Permalink  
Comments
valery possoz 18-Feb-13 17:14pm
   
I got the same as you, there's nothing wrong with the code itself, the problem is a bad using. See my answer.

Valery.
Matej Hlatky 18-Feb-13 17:27pm
   
Yes, the wordprocessingml vs. drawingml.
Good point, Valery!
m.kashif.ashraf 20-Feb-13 1:53am
   
Thanks Valery and Matej for your timely contribution, Valery's solution worked.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100