Generating email message with Outlook 2003 and Word 2003

A

ali

I am running the sample provided by MS Knowledge Base Article and I
get the following error:

"An unhandled exception of type 'System.NullReferenceException'
occurred in Outlook Email with Word.exe

Additional information: Object reference not set to an instance of an
object.

The error is generated upon execution of the following line:

//Automate the Outlook mail item.
Outlook.MailItemClass mItem = (Outlook.MailItemClass)
doc.MailEnvelope.Item;

Any hints that you might have would be greatly appreciated. The full
sample is provided below:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
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;
using System.Diagnostics;


namespace Outlook_Email_with_Word
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///

public class MyApi
{
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string
strWindowName);
};

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Word.Application wApp;


public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 88);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
CreateEmailMessage(); }

private void CreateEmailMessage()
{
//Initialize the envelope values.
string strTo = "[email protected]";
string strBCC = "[email protected]";
string strCC = "[email protected]";
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;
}

}
}
 
Top