HOWTO: Automate Outlook Using Visual C++/MFCID: Q220600
|
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++.
Follow the steps below to build and run the example:
// 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();
Outlook Version | Type Library |
---|---|
97 | msoutl8.olb |
98 | msoutl85.olb |
2000 | msoutl9.olb |
#include "msoutl85.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;
Namespace olNS(olApp.GetNames("MAPI"));
to:
_Namespace olNS(olApp.GetNames("MAPI"));
For additional information about Automation of Outlook, please see the following articles in the Microsoft Knowledge Base:
Q199870 HOWTO: Send a Message by Outlook Object Model with VC++
Q196776 FAQ: Office Automation Using Visual C++
© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Joe Crump, Microsoft Corporation
Additional query words: _com_ptr_t import COleDispatchDriver CoCreateInstance
Keywords : kbole kbAutomation kbMFC kbOutlookObj kbVC kbVC500 kbVC600 kbGrpDSO kbOffice2000 kboutlook2000
Version : WINDOWS:2000,98; :; winnt:5.0,6.0
Platform : WINDOWS winnt
Issue type : kbhowto
Last Reviewed: July 13, 1999