Introduction
I am new to the C# world and my boss can't wait for the job to get done. So I was forced to start the work before I knew how to do it.
I have a project where I need to pass some values from a datatable to an existing Word 2007 document, and print it. I spent the last few days looking for some samples where I could see how to do that, but none of those samples did what I needed.
I was forced to try some of the C# namespaces and see if they do that.
If you are trying to do something like this, I think that the code I present here will help you.
Using the Code
There are some ways to edit a Word 2007 document: In Microsoft articles (at least the ones I've read about the subject), you are forced to understand the "*.docx" format. If you understand that the underlying format is XML in a zipped package, you are half way to understanding the way to edit it, but I needed something simpler: my task was simple and what I needed was simple automation.
Below is all the code you need to open a Word 2007 document, change some sentences (with further exploitation, you can add tables, change formats, ...), print the document, and even open your mail client to send this document to someone as an attachment.
private void met_word_automation()
{
try
{
object varFileName = "c:\temp\doc.docx";
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
string varText;
Microsoft.Office.Interop.Word.Application varWord =
new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document varDoc =
varWord.Documents.Open(ref varFileName, ref varMissing,
ref varFalseValue,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing);
varDoc.Activate();
varText = "Altered sentence nr. 1";
varDoc.Sentences[0].Text = varText;
varText = "Altered sentence nr. 3";
varDoc.Sentences[2].Text = varText;
varDoc.Save();
varWord.Visible = true;
Microsoft.Office.Interop.Word.Dialog varDlg =
varWord.Application.Dialogs[
Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint];
varDlg.Show(ref varMissing);
varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing,
ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varFalseValue, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing);
varDoc.SendMail();
}
catch (Exception varE)
{
MessageBox.Show("Error:\n" + varE.Message, "Error message");
}
}
You need to create references to this .NET namespace in order to use the above code:
Microsoft.Office.Interop.Word
This namespace is in the Msword.olb COM library which is usually in the folder C:\Program Files\Microsoft Office\Office12.
Add the reference this way: Menu 'Project', 'Add reference', then select 'COM' separator and search for that library.
I hope this code will be of some use to you.
P. S. By IRRDEV request, you can download a Visual Studio 2005 project with a form. This form has a button that opens a Word doc and replaces every paragraph with other text. The path for the document is hard-coded inside the button click event, you must change the path to the Word file.
History
- 8th May, 2007: Initial post