Microsoft KB Archive/819398

= How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited =

Article ID: 819398

Article Last Modified on 1/17/2007

-

APPLIES TO


 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-





SUMMARY
This article describes how to set the correct project references to the following object libraries by using Microsoft Visual C# .NET:
 * Microsoft Outlook 10.0/11.0 Object Library
 * Microsoft Word 10.0/11.0 Object Library

This article then describes how to use these object libraries to create a pre-populated e-mail message that you can edit.



IN THIS TASK

 * INTRODUCTION
 * Create an automation client for Word and for Outlook
 * Test the automation client
 * REFERENCES



INTRODUCTION
Automation is a process that permits applications that are written in languages, such as Visual C# .NET, to programmatically control other applications. When you automate Word, Outlook, and other Microsoft Office applications, most actions that you can perform manually through the user interface you can also perform programmatically by using automation.

back to the top

Create an automation client for Word and for Outlook
To create an automation client for Word and for Outlook, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the Visual C# Projects types list, click Windows Application.

By default, Form1 is created. Add a reference to the Word Object Library and to the Outlook Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, and then locate Microsoft Outlook 10.0/11.0 Object Library and Microsoft Word 10.0/11.0 Object Library.</li> Click each library, and then click Select.

Note Microsoft recommends that you download and then install the Microsoft Office XP Primary Interop Assemblies (PIAs). See article 328912 in the &quot;References&quot; section.</li> In the Add References dialog box, click OK.</li> Click Yes if you are prompted to generate wrappers for the libraries that you selected.</li></ol> </li> Add an HTML document to the project, and then put the HTML document in the bin folder. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click the project icon.</li> Click Add, and then click New Item.</li> Under Templates, click HTML Page, and then name the page HTMLPage1.htm.</li> In the HTMLPage1.htm document, type Here is an e-mail from my new application, and then save the HTMLPage1.htm document in the bin folder.</li> Expand the bin folder so that you can see the debug folder. Move the HTMLPage1.htm document to the debug folder.</li></ol> </li> On the View menu, click Toolbox. Add a button to Form1.</li> Double-click Button1.

The code window for the form appears.</li>  Locate the following code in the code window: private void button1_Click(object sender, System.EventArgs e) { } Replace the previous code with the following code: private void button1_Click(object sender, System.EventArgs e) { CreateEmailMessage; } </li>  Add the following function to the code window: private void CreateEmailMessage {           //Initialize the envelope values. string strTo = &quot;yourname@yourdomain.com&quot;; string strBCC = &quot;yourname@yourdomain.com&quot;; string strCC = &quot;yourname@yourdomain.com&quot;; string strSubject = &quot;Outlook Automation&quot;; string strBody = &quot;HTMLPage1.htm&quot;;

//Automate the Word document. wApp = new Word.Application; wApp.Visible = false; object template = System.Reflection.Missing.Value; object newTemplate = System.Reflection.Missing.Value; object documentType = Word.WdNewDocumentType.wdNewEmailMessage; object visible = false; wApp.Visible = false; Word._Document doc = wApp.Documents.Add(               ref template,                ref newTemplate,                ref documentType,                ref visible);

//Automate the Outlook mail item. Outlook.MailItemClass mItem = (Outlook.MailItemClass)doc.MailEnvelope.Item; mItem.To = strTo; mItem.BCC = strBCC; mItem.CC = strCC; mItem.Subject = strSubject; mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; mItem.HTMLBody = GetString(strBody); mItem.ItemEvents_Event_Close += new Outlook.ItemEvents_CloseEventHandler(this.wApp_Close);

wApp.Visible = true; // Loop until there are no more references to release. while (Marshal.ReleaseComObject(mItem) > 0); mItem = null;

// Invoke the .NET garbage collector. GC.Collect; GC.WaitForPendingFinalizers; }       // Close the Word application after the message has been sent. private void wApp_Close(ref bool e)       { object oMissing = System.Reflection.Missing.Value; wApp.Quit(ref oMissing,ref oMissing,ref oMissing); }

// Get the body of the e-mail. private string GetString(string filename) {           string strFileStreamText = &quot;&quot;; string strTempRead = &quot;&quot;; try {               if(&quot;&quot; == filename) {                   filename = &quot;HTMLPage1.htm&quot;; }               System.IO.Stream fin = File.OpenRead(filename); System.IO.StreamReader sr = new System.IO.StreamReader(fin); strTempRead = sr.ReadToEnd; strFileStreamText = strFileStreamText + strTempRead; sr.Close; }           catch(Exception exp1) {               MessageBox.Show(exp1.Message); }           return strFileStreamText; } </li>  Add the following class in the working namespace before the Form1 class.

You do this to close the Word application after the message is sent. private Word.Application wApp; public class MyApi {       [DllImport(&quot;user32.dll&quot;)] public static extern int FindWindow(string strclassName, string strWindowName); }; </li>  Move to the top of the code window. Add the following code to the end of the list of the using directives: using Microsoft.Office; using Microsoft.Office.Core; using System.Runtime.InteropServices; using Outlook = Microsoft.Office.Interop.Outlook; using Word = Microsoft.Office.Interop.Word; using System.IO; </li></ol>

back to the top

Test the automation client

 * 1) Press F5 to build and to run the program.
 * 2) On the form, click Button1.

The program starts Word and Outlook, creates an e-mail that can be edited, and then populates the e-mail with the HTML page.

back to the top

<div class="references_section">