locked
create Microsoft Word Document in C# RRS feed

  • Question

  • I am trying to create Microsoft Word Document in C# using this article and other articles but it's not working! the Word.Application is not recognized! do I have to add a using declaration as well?
    Jassim Rahma
    Friday, March 26, 2010 10:17 PM

Answers

  • Hi, Jassim:

        Just a quick reflection. If you want to tackle the doc format in code, I am afraid that you have to check the Word documetn file format specification. Though it's not impossible, it could be tedious. Why re-invent the wheel instead of living happily with the Microsoft Word, or some third-party compatible softwares?


    You should only consider the OpenXML format office documents.  Convert the binary format files to OpenXML format.  The OpenXML SDK's are the best reference for OpenXML. 
    • Marked as answer by SamAgain Monday, March 29, 2010 11:06 AM
    Monday, March 29, 2010 6:31 AM
  • Exactly, you really should look at openXML SDK. The sdk comes with a tool that allows you to open a docx document (containing images, tables and whatever you might wish) and create the source code to generate that file! 
    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by SamAgain Monday, March 29, 2010 11:06 AM
    Monday, March 29, 2010 8:39 AM

All replies

  • If you followed the instructions in the article you cited, your question is unusual.  You really need to follow the instructions step-by-step.  Have you writen the using statements required by the article?
    Friday, March 26, 2010 10:27 PM
  • thanks it's working now but one question... where can i get a a step-by-step or reference on how to create full document including tables and pictures and font formatting using C#?


    Jassim Rahma
    Saturday, March 27, 2010 10:29 PM
  • Hi, Jassim:

        Just a quick reflection. If you want to tackle the doc format in code, I am afraid that you have to check the Word documetn file format specification. Though it's not impossible, it could be tedious. Why re-invent the wheel instead of living happily with the Microsoft Word, or some third-party compatible softwares?


    Please mark the right answer at right time.
    Thanks,
    Sam
    Monday, March 29, 2010 6:12 AM
  • Hi, Jassim:

        Just a quick reflection. If you want to tackle the doc format in code, I am afraid that you have to check the Word documetn file format specification. Though it's not impossible, it could be tedious. Why re-invent the wheel instead of living happily with the Microsoft Word, or some third-party compatible softwares?


    You should only consider the OpenXML format office documents.  Convert the binary format files to OpenXML format.  The OpenXML SDK's are the best reference for OpenXML. 
    • Marked as answer by SamAgain Monday, March 29, 2010 11:06 AM
    Monday, March 29, 2010 6:31 AM
  • Exactly, you really should look at openXML SDK. The sdk comes with a tool that allows you to open a docx document (containing images, tables and whatever you might wish) and create the source code to generate that file! 
    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by SamAgain Monday, March 29, 2010 11:06 AM
    Monday, March 29, 2010 8:39 AM
  • Monday, March 29, 2010 5:10 PM
  • You can probably do everything with the .NET framework classes, but you need the SDK documentation to know what to do.
    Monday, March 29, 2010 5:37 PM
  • well, OpenXML is great.. I never used it before and it seems to be easier to convert from XML into a formatted Microsoft Word Document using OpenXML.

    I will play with it and I am sure I will need your help while starting..

    Thanks...


    Jassim Rahma
    Monday, March 29, 2010 9:32 PM
    • Edited by SamAgain Tuesday, March 30, 2010 6:44 AM refine
    Tuesday, March 30, 2010 6:42 AM
  • just a question which I don't understand: does OpenXML has a different format other than .xml elements and nodes or it's just a gateway between xml and office products? and if it has it's own format then how can I create a basic OpenXML raw file using C#?
    Jassim Rahma
    Tuesday, March 30, 2010 11:18 PM
  • Hi, Jassim:

         Office Open XML (also informally known as OOXML or Open XML) is an ISO/IEC standardized ZIP-based file format originally developed by Microsoft for representing spreadsheets, charts, presentations and word processing documents. Starting with Microsoft Office 2007, the Office Open XML file formats (ECMA-376) have become the default file format of Microsoft Office.

         OpenXML has its own elements and tags, which is understandable because of the extensibility of the XML. There's an OpenXML SDK to help you dealing with all the Office documents format and the link below could be a good start.

         Open XML Format SDK 2.0: Getting Started Best Practices

         If you haver further questions related to the office file format, Office File Formats forum could be a better option.

     
    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Wednesday, March 31, 2010 2:30 AM refine
    Wednesday, March 31, 2010 2:22 AM
  • Hi,

    you can also try this C# / VB.NET Word library if you want an easy way to C# create Word documents.

    Here is a sample C# code:

    // Create a new empty document.
    DocumentModel document = new DocumentModel();
     
    // Add document content.
    document.Sections.Add(
    	new Section(document,
    		new Paragraph(document, "Hello World!")));
     
    // Save the document to a file.
    document.Save("Document.docx", SaveOptions.DocxDefault);
     
    // Open the document file with Microsoft Word.
    Process.Start("Document.docx");

    Friday, May 4, 2012 10:40 AM