VC 操作 outlook的方法

来源:互联网 发布:经济学的大数据 编辑:程序博客网 时间:2024/04/29 05:41
 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;
 }

}

原创粉丝点击