Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create the .docx document with Microsoft.Office.Interop.Word?

How do I create the .docx document with Microsoft.Office.Interop.Word from List? or the best way is to add docx.dll?

http://www.c-sharpcorner.com/UploadFile/scottlysle/using-the-docx-dll-to-programmatically-create-word-documents/

Update. May be my first question is a litle incorrect. What is the difference between Microsoft.Office.Interop.Word and DocX.dll? Do I need Microsft Word for creating and opening .docx document in both cases?

like image 461
novicegis Avatar asked Oct 29 '13 11:10

novicegis


People also ask

How do I make a DOCX file in Word?

Once you have chosen an appropriate location, enter a file name in the 'File name' field. From the 'Save as type' dropdown, ensure 'Word Document (*. docx)' is selected. Click 'Save' to confirm and save the file.

Is Word XML document DOCX?

The Office Open XML-based word processing format using . docx as a file extension has been the default format produced for new documents by versions of Microsoft Word since Word 2007.

Where can I create a DOCX file?

There are two avenues to creating a DOCX template: From the Author Tab on the Word Ribbon (when you edit an existing DOCX document) From Workspace Explorer.


1 Answers

After installing OpenXML SDK you will able to reference DocumentFormat.OpenXml assembly: Add Reference -> Assemblies -> Extensions -> DocumentFormat.OpenXml. Also you will need to reference WindowsBase.

Than you will be able to generate document, for example, like this:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var document = WordprocessingDocument.Create(
                "test.docx", WordprocessingDocumentType.Document))
            {
                document.AddMainDocumentPart();
                document.MainDocumentPart.Document = new Document(
                    new Body(new Paragraph(new Run(new Text("some text")))));
            }
        }
    }
}

Also you can use Productivity Tool (the same link) to generate code from document. It can help to understand how work with SDK API.

You can do the same with Interop:

using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;

namespace Interop1
{
    class Program
    {
        static void Main(string[] args)
        {
            Application application = null;
            try
            {
                application = new Application();
                var document = application.Documents.Add();
                var paragraph = document.Paragraphs.Add();
                paragraph.Range.Text = "some text";

                string filename = GetFullName();
                application.ActiveDocument.SaveAs(filename, WdSaveFormat.wdFormatDocument);
                document.Close();

            }
            finally
            {
                if (application != null)
                {
                    application.Quit();
                    Marshal.FinalReleaseComObject(application);
                }
            }
        }
    }
}

But in this case you should reference COM type library Microsoft. Word Object Library.


Here are very useful things about COM interop: How do I properly clean up Excel interop objects?

like image 185
Evgeny Timoshenko Avatar answered Oct 17 '22 23:10

Evgeny Timoshenko