1
votes

I would like to open a Word document in C#. I mean I would like to know how a Word document is embedded in C#.

I found some article in the net and I have a question. What does it mean here: Open Word Application, and Add New. If I open the Word I don't see any "add new" is that in C#? I'm using Microsoft Visual Studio 2008.

All the methods used Word automation is derived either from Word.Application or Word.Document class.

Let's consider that we want to create a document using the Word Application, we might end up doing the following steps, Open Word Application. (Opening Word Application creates a new document by default, but in Automation, wee need to manually add a document)

Add a New document.
Edit the document.
Save it

You can find the article here by the way: http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx and I would like to make it clear that this is not a homework.

4
Homework? If so, you should tag is as such.Fredrik Mörk
This is not a homework Fredrik. I would like to know where is this add a new document in the Open Word Application. I would like to know how to integrate the word into Microsoft Visual Studio Programming. And if I open in my program the word document. I'm not a type of person who like to ask somebody to do my homework, because I usually do it on my own way. And besides I'm done with my bachelor and I'm just learning programming for the first time & I'm not in school anymore.tintincutes
Try recording Macros in Word and looking at the code that it generates. The object models between the macros and C# are similar and it's a great way to get started, especially if this is your first programming attempt. Once you learn the basics you can switch over to visual studio.Ken

4 Answers

8
votes

I tended to find the Open XML SDK much better to create Word documents with, as it doesnt require an instance of Word or Excel.

Open XML Format SDK V1

Open XML Format SDK V2

Version 2 is much much better than version 1.

0
votes

One good trick to see how a document is built up is to go to File > Save As > Xml Document. This is how I learned to build out excel files on the fly, and I'm sure it works just fine for Word as well. I'm using office 2003 so I'm not sure if that save feature exists, but I konw that the excel documents I create are compatible with 2007.

0
votes

As above.

Aslo, I've done quite a bit of driving Office applications using .NET. My advice is if you know VB refactor the library into a VB.NET DLL. VB.NET supports optional parameters and late binding unlike C#, which makes it much easier for automating office.

0
votes

Follow my code, be sure to build a GUI with a button to press.

Code button:

 private void button1_Click(object sender, EventArgs e)
        {
            CreateDocument();
        }

Build the word doc:

 //Create a new document
                Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);


 //Save the document
                object filename = @"C:\Users\xxxx.xxxxxxx\Desktop\program debug\xxxxxxxxx";
                document.SaveAs(ref filename);
                document.Close(ref missing, ref missing, ref missing);
                document = null;
                winword.Quit(ref missing, ref missing, ref missing);
                winword = null;
                MessageBox.Show("Document created successfully !");