分享
 
 
 

How To Automate Outlook Using Visual C++/MFC

王朝vc·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

How To Automate Outlook Using Visual C++/MFCView products that this article applies to.

Article ID

:

220600

Last Review

:

June 29, 2004

Revision

:

1.0

This article was previously published under Q220600

On This Page

SUMMARY

MORE INFORMATION

Automating Microsoft Outlook 2000, 2002, and 2003

REFERENCES

APPLIES TO

SUMMARYYou 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++.

Back to the top

MORE INFORMATIONFollow 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 Library97

msoutl8.olb

98

msoutl85.olb

2000

msoutl9.olb

2002

msoutl.olb

2003

msoutl.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.

Back to the top

Automating Microsoft Outlook 2000, 2002, and 2003You 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.

Back to the top

REFERENCESFor 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++

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有