How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited (819398)
The information in this article applies to:
- Microsoft Office Outlook 2003
- Microsoft Outlook 2002
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
- Microsoft Office Word 2003
- Microsoft Word 2002
SUMMARYThis 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.INTRODUCTIONAutomation 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 topCreate an automation client for Word and for OutlookTo 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.
- 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 "References" section. - In the Add References dialog box, click OK.
- Click Yes if you are prompted to generate wrappers for the libraries that you selected.
- Add an HTML document to the project, and then put the HTML document in the bin folder. To do this, follow these steps:
- In Solution Explorer, right-click the project icon.
- Click Add, and then click New Item.
- Under Templates, click HTML Page, and then name the page HTMLPage1.htm.
- 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.
- Expand the bin folder so that you can see the debug folder. Move the HTMLPage1.htm document to the debug folder.
- On the View menu, click Toolbox. Add a button to Form1.
- Double-click Button1.
The code window for the form appears. - 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(); }
- Add the following function to the code window:
private void CreateEmailMessage()
{
//Initialize the envelope values.
string strTo = "yourname@yourdomain.com";
string strBCC = "yourname@yourdomain.com";
string strCC = "yourname@yourdomain.com";
string strSubject = "Outlook Automation";
string strBody = "HTMLPage1.htm";
//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 = "";
string strTempRead = "";
try
{
if("" == filename)
{
filename = "HTMLPage1.htm";
}
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;
}
- 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("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);
}; - 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;
back to the topTest the automation client- Press F5 to build and to run the program.
- 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 topREFERENCES
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
311452
INFO: Develop Microsoft Office solutions with Visual Studio .NET
For additional information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
328912
INFO: Microsoft Office XP PIAs are available for download
For additional information about Microsoft Office development with Visual Studio, visit the following Microsoft Developer Network (MSDN) Web site: For additional information about installing and distributing PIAs with your solution, see the "Distributing solutions that rely on the Office XP PIAs" section of the PIA Readme.txt file. You can find the PIA Readme.txt file at the Office XP Primary Interop Assemblies download site that is mentioned in article 328912. back to the top
Modification Type: | Major | Last Reviewed: | 4/18/2006 |
---|
Keywords: | kbHOWTOmaster kbWindowsForms kbPIA kbhtml kbenvelope kbProgramming kbenv kbemail kbAppDev kbSample kbMsg kbOutlookObj kbinterop kbAutomation KB819398 kbAudDeveloper |
---|
|