How To Automate Outlook Using Visual C++/MFC

How To Automate Outlook Using Visual C++/MFC

Article ID:220600
Last Review:June 29, 2004
Revision:1.0
This article was previously published under Q220600

SUMMARY

You can programmatically control Microsoft Outlook using Microsoft Visual C++. This article demonstrates how to create contacts, create appointments, and send messages using Microsoft Outlook's object-model from Visual C++.

MORE INFORMATION

Follow the steps below to build and run the example:
1.Start Visual C++ and create a new MFC EXE dialog-based application.
2.Add a button to your dialog.
3.Double-click the button to add a handler for it, and add the following code:
   // Start Outlook.
   // If it is already running, you'll use the same instance...
   _Application olApp;
   COleException e;
   if(!olApp.CreateDispatch("Outlook.Application", &e)) {
      CString str;
      str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
      AfxMessageBox(str, MB_SETFOREGROUND);
      return;
   }

   // Logon. Doesn't hurt if you are already running and logged on...
   NameSpace olNs(olApp.GetNamespace("MAPI"));
   COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
   olNs.Logon(covOptional, covOptional, covOptional, covOptional);

   // Create and open a new contact
   _ContactItem olItem(olApp.CreateItem(2));

   // Setup Contact information...
   olItem.SetFullName("James Smith");
   COleDateTime bdDate;
   bdDate.SetDate(1975, 9, 15);
   olItem.SetBirthday(bdDate);
   olItem.SetCompanyName("Microsoft");
   olItem.SetHomeTelephoneNumber("704-555-8888");
   olItem.SetEmail1Address("someone@microsoft.com");
   olItem.SetJobTitle("Developer");
   olItem.SetHomeAddress("111 Main St./nCharlotte, NC 28226");

   // Save Contact
   olItem.Save();

   // Create a new appointment
   _AppointmentItem olAppt(olApp.CreateItem(1));

   // Schedule it for two minutes from now...
   COleDateTime apptDate = COleDateTime::GetCurrentTime();
   olAppt.SetStart((DATE)apptDate + DATE(2.0/(24.0*60.0)));

   // Set other appointment info...
   olAppt.SetDuration(60);
   olAppt.SetSubject("Meeting to discuss plans...");

   olAppt.SetBody("Meeting with James to discuss plans.");
   olAppt.SetLocation("Home Office");
   olAppt.SetReminderMinutesBeforeStart(1);
   olAppt.SetReminderSet(TRUE);

   // Save Appointment
   olAppt.Save();

   // Prepare a new mail message
   _MailItem olMail(olApp.CreateItem(0));
   olMail.SetTo("someone@microsoft.com");
   olMail.SetSubject("About our meeting...");
   olMail.SetBody(
      "Hi James,/n/n"
      "/tI'll see you in two minutes for our meeting!/n/n"
      "Btw: I've added you to my contact list!");

   // Send the message!
   olMail.Send();

   AfxMessageBox("All done.", MB_SETFOREGROUND);
   olNs.Logoff();
						
4.Bring up the ClassWizard (Control-W) click the Automation Tab, and choose "From a type library" under the Add Class menu.
5.In the dialog box that comes up, navigate to the directory where Outlook is installed, and choose the Outlook type library (see table below). Select all the items it finds, and click OK to have ClassWizard generate MFC wrapper classes for all of them:
Outlook VersionType Library
97msoutl8.olb
98msoutl85.olb
2000msoutl9.olb
2002msoutl.olb
2003msoutl.olb

6.Add the following just before the implementation of your button handler:
   #include "msoutl85.h" // for Outlook 2000 use msoutl9.h
                         // for Outlook 2002 & Outlook 2003 use msoutl.h

   // Ole-initialization class.
   class OleInitClass {
   public:
      OleInitClass() {
         OleInitialize(NULL);
      }
      ~OleInitClass() {
         OleUninitialize();
      }
   };
   // This global class calls OleInitialize() at
   // application startup, and calls OleUninitialize()
   // at application exit...
   OleInitClass g_OleInitClass;
						
7.Compile and run the project. Once it has run, you should have a new contact named James Smith, an appointment scheduled in two minutes with a reminder to appear in one minute, and have sent a message to someone@microsoft.com. Also, because you added a birthday for your contact (9/15), a recurring event was added for your Outlook Calendar to remind you on that day.

Automating Microsoft Outlook 2000, 2002, and 2003

You can use the sample code previously described to automate Outlook 2000, 2002, and 2003 with one small change. The Outlook 97 Namespace class member has changed to _Namespace in Outlook 2000, 2002, and 2003. To use the code above for automating Outlook 2000, 2002, and 2003 change this line:
Namespace olNS(olApp.GetNames("MAPI"));
				
to:
_Namespace olNS(olApp.GetNames("MAPI"));
				
A new virus protection feature of Outlook 2002 and Outlook 2003 causes a dialog box to appear advising you that a program is using your address list and sending e-mail from your computer. It asks your approval to continue.

REFERENCES

For additional information about automation of Outlook, click the following article numbers to view the articles in the Microsoft Knowledge Base:
199870 (http://support.microsoft.com/kb/199870/) How To Send a Message by Outlook Object Model with VC++
196776 (http://support.microsoft.com/kb/196776/) Office Automation Using Visual C++
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值