Generating email message with Outlook 2003 and Word 2003

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

The error is generated upon execution of the following line:

//Automate the Outlook mail item.
Outlook.MailItemClass mItem = (Outlook.MailItemClass)

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
		public static extern int FindWindow(string strclassName,   string

	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

			// 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) 
			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();
			// 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.Name = "Form1";
			this.Text = "Form1";


		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		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 = "";
			string strBCC = "";
			string strCC = "";
			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)
			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

			wApp.Visible = true;

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

			// Invoke the .NET garbage collector.

		// 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 = "";
				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;
			catch(Exception exp1)
			return strFileStreamText;

ali9046 (1)
7/8/2004 12:29:55 AM
outlook 87535 articles. 11 followers. Follow

0 Replies

Similar Articles

[PageSpeed] 9


Similar Artilces:

pgp error when opening Outlook 2000 Pro.
Please help. I have a computer that is running Windows XP Home and MSOffice 2000 Pro. Anyhow, when I try to open Outlook, it gives an error "pgpexch.dll" and says that virtual memory may be set to low. I don't know if the problem is with the file or with the virtual memory settings. I've gone into the virtual memory and set it to let Windows manage it but I still get the error. What can I do? Thanks in advance for your help. Sincerely, Rodney Brown ...

Word 2007
Hi, I am doing a multi-chapter project and have been attempting to put endnotes at the end of each chapter. I have had no success. Can this be done? If so, how? Thank you for your help. Insert a section break at the end of each chapter and set the endnotes options to insert the notes at the end of the section rather than at the end of the document. You can access the options by clicking the little arrow in the bottom right corner of the Footnotes group on the References tab of the ribbon. -- <>>< ><<> ><<> <>>< ><<&...

outlook over RPC new auth behaviour introduced?
I've been trying to isolate a strange issue with OORPC. Prior to some recent patching, a user would be able to log on to outlook over RPC without a workstation being a member of the domain by installing the relevant certificates and configure and setting up outlook as usual. Additionally your local cached login ID (say for example, a domain laptop was offsite and you logged in with cached info) did not seem related to whether you could log into an outlook over RPC account for a different user so long as you were using different outlook profiles. Now it seems that OORPC is only work...

Incoming Emails 01-15-10
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Email Client: pop Why am I suddenly getting the same emails sent to me over and over again whenever I log on? Every time I log on, I now get emails from last week that I've already received and even deleted. On 1/14/10 5:47 PM, in article 59bb0f88.-1@webcrossing.JaKIaxP2ac0, "" <> wrote: > Why am I suddenly getting the same emails sent to me over and over again > whenever I log on? Every time I log on, I now get emails from last week that > I've al...

How do you set up a submit button to attach worksheet in email?
I am doing a form and I want to have a submit or send link on the bottom of the page. I know how to make hyperlinks but I dont know how to get it where when the user hits the send button it automatically saves that info and sends that form to the intended email address. Basically so you dont have to attach it yourself through email. Is there a way to do this? Have a look here for code to send mail -- Paul B Always backup your data before trying something new Please post any response to the newsgroups so others can benefit from it Feedback on a...

Excel 2003/edit/replace. Under Options/Look In, values is AWOL bu.
Under Look In options Find allows for search by formulae OR values OR comments Replace is restricted to formulaes ONLY WHY? cp Have a look at this google search result. This topic came up a couple days ago and was addressed by Dave and Myrna. Gord Dibben Excel MVP On Fri, 28 Jan 2005 03:49:02 -0800, "cp" <> wrote: >Under Look In options >Find allows for search by formulae OR values OR comments >Replace is restricted to formulaes ONLY >WHY? ...

Leaving messages on server
Where do I select the option to leave messages on server so I can view them on another computer? In the advanced properties of your POP mail account settings. Without knowing your version of Outlook, hard to tell you more. Troy wrote: > Where do I select the option to leave messages on server > so I can view them on another computer? ...

Cannot read Base64 encoded emails (OL98)
Hello folks, I have a problem with one of my Outlook 98 machines. One user (Ironically the boss!) cannot read base64 encoded emails, they come up as garbage, which I then have to run through a Base64 decoder. Other users also use Outlook 98, but can read the emails fine. the problem affects the message text and encoded objects. Mail is coming from the same local server (602 ProLan Suite). The machine with the error is running build 8.5.7806.0. The other machines (Which read messages fine) are both running build 8.5.5104.6. This leads me to thinking that the problem is a result of a ...

Synchronise Outlook 2003
I am using outlook 2003 on a laptop. I would like to sync my changes with my assistant outlook 2003 when I get back to the office. Is it possible to sync contacts ? I do not want to use a central data base but really sync 2 different data bases. thanks phil <> wrote: > I am using outlook 2003 on a laptop. I would like to sync my changes > with my assistant outlook 2003 when I get back to the office. Is it > possible to sync contacts ? > I do not want to use a central data base but really sync 2 different > data bases.

Setting up Exchange 2003 in a new environment
Hi Guys/Gals, I need some guidance as I am preparing to setup a new domain and exchange enviroment. this company currently has no internal domain structure, and no email structure at all. however their website/domain is being hosted through service provider, which has included a certain # of email accounts that can be used, via POP3 and SMTP. for these email addresses to work with the new exchange, what are some of the steps that I need to ensure that I use, in order for email to properly work internally and externally? I know that I need to have a DC structure put into place first, with ...

Sending email but outbox is empty
Hi NG, When I press send/receive Outlook shows in its status bar that it works on sending emails (Sending messages 1 of 4) but there is not mail in outbox. As a result I currently cannot send any other mails since Outlook keeps working on the 4 emails I don't see. Any ideas, I am clueless? - Thanks a lot for any help!!!! Dirk ...

set up email 07-14-10
hello.i am trying to set up an email account on my laptop and my modem is my Blackberry phone.everytime i try to set it up it comes back with all kinds of email on the phone works great,but i would rather have an account on my phone is T-Mobile Blackberry Bold.i would be very thankful for any help. "Looster59" <> wrote in message > hello.i am trying to set up an email account on my laptop and my modem is > my > Blackberry phone.everytime...

Bulk Import Wizard for Leads Issue
Hi, I am trying to import Lead records using a CSV file containing around 3,000 records. I have tested with around 20 records (taken from the larger file) and it imports fine but when I select the the 3,000 record file for import the wizard gives an error message somthing like (sorry don't have exact wording) 'Source Import file is corrupt'. Are there any known issues with CSV files such as the following : 1) Special characters such as an asterix not handled 2) Email and Website type fields must all be correctly formatted? 3) File is too large Thanks for your help in adva...

Outlook 2003 attach File Browse Window Stops Responding
In outlook 2003 when I try to attach a file and the browse window pops open to browse for the file I want to attach the window will stop responding and I have to force quit the outlook. Any Ideas? do you have mapped network drives? how long do you wait before quitting outlook? -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Author, Google and Other Search Engines (Visual QuickStart Guide) Outlook Tips: Outlook & Exchange Solutions Center: http://www.slipsti...

new email notification doesn't work?
In Outlook 2003, I have the option set for the semi-transparent notification window to pop up when I get new email, but it never does. Is there something I'm not doing right? Does it only come up in certain situations? Btw, I'm using the regular Inbox with a POP3 account. -- John Salerno Do you have rules moving your email to folders after receipt? --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert latest virus name here) virus, all mail sent to my personal account will be deleted without reading. After searchi...

Sharing Outlook #2
I want to set up three account on my outlook. I have already set up two of them. Is there any way of switching from profile to profile while in outlook? nope...afaik... "PaulaDawn" <> wrote in message news:OHC8VWbSEHA.3692@TK2MSFTNGP10.phx.gbl... > I want to set up three account on my outlook. > I have already set up two of them. Is there any way of switching from > profile to profile while in outlook? > > Only by shutting down and restarting - switching MAPI profiles is not as easy as switching identities in Outlook Express. --� Milly Stap...

Outlook 2003 cuts off lines
Hi! I synchronize my PDA daily and as part of its software, Newsbreak, I can send myself emails of interesting stories with URL links to look up online later when I get home and connect. When I load them into Outlook 2003 and send them to myself, very long URLs appear correct in the Outbox version of the emails. When I end up receiving the emails back INTO Outlook, it cuts off lines beyond a certain length, thereby rendering the URLs broken. Now I can look in the Sent Items and click the good version of the URL but I would really love to know how you keep Outlook 2003 from hatcheting...

Microsoft Money 2003
When I try to print a chart or report, I get the message "This program has caused a default, etc." and it shuts down. Can you give me some hints on how to correct this? Thanks Is this all reports or a particular one? If it's only one, try resetting the report. If that doesn't help, try it in a new file. If it does it in multiple files, you may need to try updating your printer driver or removing and reinstalling Money. "Mary J" <> wrote in message news:035001c377cc$e2e77840$a301280a@phx.gbl... > When I try to print a chart or re...

IMC Generates NDR with Mail From: <>
Hi all IMC Generates NDR with Mail From: <> . how to solve this problem Thanks Shaikh ...

Outlook user profiles
I set-up two different user profiles in outlook. I am not able to change the settings in one user profile under Tools/options/mailset-up without it changing the settings on the other user profile. I need to apply individual settings for each. Anyone know how to remedy this? I have tried everything! Which settings are youi trying to change? -- Sue Mosher, Outlook MVP Author of Microsoft Outlook Programming - Jumpstart for Administrators, Power Users, and Developers "gia" <> wrot...

Report Designer on Visual Studio 2005: Is it possible without 2003?
Do I have to install Visual Studio .NET 2003 to design new reports on CRM? I can't publish them on CRM when I have only the Visual Studio 2005 installed on my machine. When I install the SRS Designer, the Wizard tells me that I do not have the Visual Studio 2003. Thanks in advance! You need VS to create report rdl file. Visual Studio 2005 should work. To publish report you can log into CRM to upload your report file. "Juliano" <> wrote in message > Do I have to install Visual Studio .NET 2...

Word table to XML
Can some one help me in converting a word table to XML? I am using word 2003 and the macro should word even with word 2007. Thanks in advance. Which macro? What is the xml schema for the table? Yves "Sri Vidya" <Sri> wrote in message > Can some one help me in converting a word table to XML? I am using word > 2003 > and the macro should word even with word 2007. > Thanks in advance. > > ...

Sending URL in Outlook XP
When I try to send a page by email from IE 6, Outlook displays a message saying Outlook has blocked access becuase of potentially unsafe. Is there a work around for this. I used to be able to send the page as an icon or send the page as a link. How can I get these features back? Thanks, Mark ...

Exchange 5.5 to 2003 Migration Question #3
Hello, We are currently migrating users to Exchange 2003 from 5.5. The 5.5 server is located here and the new 2003 server is located in a different office. One user (who we have moved already) is trying to access and user's calendar, but that user is still on 5.5. They are getting the following error: unable to display the folder. The information store could not be opened Any ideas on how to make the user on 2003 (running Outlook 2003) connect to the user's calendar on the 5.5 server? The user has the "Owner" permission on the 5.5 calendar. Thanks! ...

Can't access comcast newsgroups in outlook express 6 or Outlook 2003
Hi, I had the comcast news server working in both aps, but trying to make it work with outlook 2003 as the default program to access the newsgroup reader, by removing the newsgroup account in outlook express 6, I now can't get it to connect in either ap. duh... It's completely gone from Outlook 2003, ie, where before, under the go button there was a link to news, now there is not. Outlook express 6 still has the newsgroup reader, that's how I'm sending this, but when i try to go to the comcast news server it repeatedly asks for my username and password. I deleted the a...