天天看點

VC 操作 outlook的方法

  http://blog.csdn.net/beyond748/archive/2007/08/31/1767336.aspx

//===========================================================

//vc  好像msdn裡面對這個說的不是很清楚,vb道是有執行個體,和vc很類似拉可以參考的。

我是在mfc裡面實作的

首先在stdafx.h裡面加入如下:

#import "C:/Program Files/Common Files/Microsoft Shared/OFFICE11/mso.dll" rename_namespace("Office"), named_guids

#import "C:/Program Files/Microsoft Office/Office11/MSOUTL.olb" rename_namespace("Outlook"), named_guids

using namespace Office;

using namespace Outlook;

、----------------------------------------

//讀取聯系人

void CmfcDlg::OnBnClickedOk()

{

 ::CoInitialize(NULL);

 {

  _ApplicationPtr spOutlook("Outlook.Application");

  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");

  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderContacts);

  _ItemsPtr spItems = spFolder->GetItems();

  _ContactItemPtr spContact;

  char s[200]={0};

  for(int i=1;i<=spItems->Count;++i)

  {

   spContact = spItems->Item(i);

   sprintf(s, "%s%s", _com_util::ConvertBSTRToString(spContact->Subject), _com_util::ConvertBSTRToString(spContact->MobileTelephoneNumber));

   AfxMessageBox(_T(s));

  }

  spContact = NULL;

  spItems  = NULL;

  spFolder = NULL;

  spNamespace = NULL;

  spOutlook = NULL;

 }

 ::CoUninitialize();

}

//讀取約會

void CmfcDlg::OnBnClickedButton2()

{

 ::CoInitialize(NULL);

 {

  _ApplicationPtr spOutlook("Outlook.Application");

  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");

  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderCalendar);

  _ItemsPtr spItems = spFolder->GetItems();

  _AppointmentItemPtr spAppointment;

  char s[1024]={0};

  for(int i=1;i<=spItems->Count;++i)

  {

   spAppointment = spItems->Item(i);

   COleDateTime t(spAppointment->Start);

   CString str1 = t.Format("%Y-%m-%d(%A) %H:%M");

   AfxMessageBox(str1);

   COleDateTime t1(spAppointment->End);

   CString str2 = t1.Format("%Y-%m-%d(%A) %H:%M");

   AfxMessageBox(str2);

   sprintf(s, "主題:%s/n地點%s/n開始時間%s/n結束時間%s/n内容:%s",

    _com_util::ConvertBSTRToString(spAppointment->Subject), _com_util::ConvertBSTRToString(spAppointment->Location),

    str1, str2,

    _com_util::ConvertBSTRToString(spAppointment->Subject));

  }

  spAppointment= NULL;

  spItems  = NULL;

  spFolder = NULL;

  spNamespace = NULL;

  spOutlook = NULL;

 }

 ::CoUninitialize();

}

void CmfcDlg::OnBnClickedCancel()

{

}

//寫入聯系人

void CmfcDlg::OnBnClickedButton1()

{

 ::CoInitialize(NULL);

 {

  _ApplicationPtr spOutlook("Outlook.Application");

  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");

  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderContacts);

  _ItemsPtr spItems = spFolder->GetItems();

  _ContactItemPtr spContact;

  spContact = spItems->Add();

  spContact->put_FullName(_com_util::ConvertStringToBSTR(str_name));

  spContact->put_MobileTelephoneNumber(_com_util::ConvertStringToBSTR(str_phoneno));

  spContact->Save();

  spContact = NULL;

  spItems  = NULL;

  spFolder = NULL;

  spNamespace = NULL;

  spOutlook = NULL;

 }

 ::CoUninitialize();

}

//寫入約會

void CmfcDlg::OnBnClickedButton3()

{

 ::CoInitialize(NULL);

 {

  _ApplicationPtr spOutlook("Outlook.Application");

  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");

  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderCalendar);

  _ItemsPtr spItems = spFolder->GetItems();

  _AppointmentItemPtr spAppointment;

  spAppointment = spItems->Add();

  spAppointment->put_Subject(_com_util::ConvertStringToBSTR("主題"));

  spAppointment->put_Location(_com_util::ConvertStringToBSTR("地點"));

  spAppointment->put_Start(GetTickCount());

  spAppointment->put_Start(GetTickCount());

  spAppointment->put_Body(_com_util::ConvertStringToBSTR("裡面的内容"));

  spAppointment->Save();

  spAppointment= NULL;

  spItems  = NULL;

  spFolder = NULL;

  spNamespace = NULL;

  spOutlook = NULL;

 }

}