Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 1:05am
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 18-Feb-13 17:27pm
   
Yes, the wordprocessingml vs. drawingml. Good point, Valery!
m.kashif.ashraf at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 800
1 OriginalGriff 503
2 Abhinav S 253
3 thatraja 245
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,282
1 OriginalGriff 4,750
2 Peter Leow 3,784
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid