如何在C++Builder中如何控制Outlook ( 积分: 50 )

  • 主题发起人 主题发起人 扶程星云
  • 开始时间 开始时间

扶程星云

Unregistered / Unconfirmed
GUEST, unregistred user!
一下是我在网上看到的,但我试验了一下,无法运行,哪位运行成功了给我个Demo.
邮箱: bingmao@@163.com
CB6中读取OUTLOOK中的地址簿
作者: 来自: 阅读次数: 6 [大 中 小]
--------------------------------------------------------------------------------

首先感谢在这方面研究过的前辈们!
express 和 outlook都能读
cpp前面要声明
#pragma link "Outlook_2K_SRVR"
#pragma link "IvDictio"
#pragma link "IvMulti"
typedef HRESULT (WINAPI *fWABOpen)(LPADRBOOK*,LPWABOBJECT*,LPWAB_PARAM,DWORD);
用了bcb的outlook控件的
代码
try{
FormList->ListView1->Clear();
_ApplicationPtr pApp;
_NameSpace* pNS;
AddressLists* pAddr;
AddressEntries * pAddrEntries;
AddressEntry* pEntry;
AddressList* pList;
int count,count1;
pApp = this->OutlookApplication1->get_Application();
pNS = pApp->GetNamespace(WideString("MAPI"));
pAddr = pNS->AddressLists;
count = pAddr->Count;
WideString email;
email.SetLength(128);
//emailname.SetLength(128);
for(int i = 1;i <= count;i++)
{
pList = pAddr->Item(TVariant(i));
pAddrEntries = pList->get_AddressEntries();
count1 = pAddrEntries->Count;
for(int j = 1;j <= count1;j++)
{
pEntry = pAddrEntries->Item(TVariant(j));
//pEntry->get_Name(&amp;emailname);
pEntry->get_Address(&amp;email);
//this->Memo1->Lines->Add(AnsiString(emailname.c_bstr()) + &quot;/t&quot;
+ AnsiString(email.c_bstr()));
TListItem * ListItem;
ListItem = FormList->ListView1->Items->Add();
ListItem->Caption = &quot;&quot;;
ListItem->SubItems->Add(AnsiString(email.c_bstr()));
ListItem->Checked = true;
}
}
ListView1->SetFocus();
ListView1->Update();
}
catch(...)
{
try{
HRESULT hRes;
//LPADRBOOK lpAdrBook;
DelphiInterface<IAddrBook> lpAdrBook;
//LPWABOBJECT lpWABObject;
DelphiInterface<IWABObject> lpWABObject;
//LPWAB_PARAM lpWABParam = NULL;
DWORD Reserved2 = NULL;
char szPath[MAX_PATH];
SHGetFolderPathX(szPath);
char * s = &quot;//System//wab32.dll&quot;;
strcat(szPath, s);
HINSTANCE hinstLib;
hinstLib = LoadLibrary(szPath);
// hinstLib = LoadLibrary(s.c_str()+&quot;//System//wab32.dll&quot;);
fWABOpen procWABOpen;
if (hinstLib != NULL)
{
// 获取&quot;Wab32.dll&quot;内部涵数WABOpen的进程地址
procWABOpen = (fWABOpen) GetProcAddress(hinstLib, &quot;WABOpen&quot;);
if (procWABOpen != NULL)
{
hRes = (procWABOpen)(&amp;lpAdrBook,&amp;lpWABObject,NULL,Reserved2);
//_ASSERTE(hRes == S_OK);
if (hRes != S_OK)
exit(1);
ULONG lpcbEntryID;
ENTRYID *lpEntryID;
hRes = lpAdrBook->GetPAB(
&amp;lpcbEntryID,
&amp;lpEntryID
);
//_ASSERTE(hRes == S_OK);
if (hRes != S_OK)
exit(2);
ULONG ulFlags = MAPI_BEST_ACCESS;
ULONG ulObjType = NULL;
//LPUNKNOWN lpUnk = NULL;
DelphiInterface<IABContainer> lpContainer;
hRes = lpAdrBook->OpenEntry(
lpcbEntryID,
lpEntryID,
NULL,
ulFlags,
&amp;ulObjType,
(LPUNKNOWN FAR *)&amp;lpContainer
);
ulFlags = NULL;
if (ulObjType == MAPI_ABCONT)
{
//IABContainer *lpContainer = static_cast <IABContainer *>(lpUnk);
LPMAPITABLE lpTable = NULL;
hRes = lpContainer->GetContentsTable(
ulFlags,
&amp;lpTable
);
//_ASSERT(lpTable);
ULONG ulRows;
hRes = lpTable->GetRowCount(0,&amp;ulRows);
//_ASSERTE(hRes == S_OK);
SRowSet *lpRows;
hRes = lpTable->QueryRows(
ulRows,// 获取所有行
0,
&amp;lpRows
);
ListView1->Clear();
for(ULONG i=0;i<lpRows->cRows;i++)
{
SRow *lpRow = &amp;lpRows->aRow;
AnsiString strTemp;
for(ULONG j=0;j<lpRow->cValues;j++)
{
SPropValue *lpProp = &amp;lpRow->lpProps[j];
if (lpProp->ulPropTag == PR_EMAIL_ADDRESS_A)
{
TListItem * ListItem;
ListItem = FormList->ListView1->Items->Add();
ListItem->Caption = &quot;&quot;;
ListItem->SubItems->Add((char *)lpProp->Value.lpszA);
//strTemp =(char *)lpProp->Value.lpszA;
ListItem->Checked = true;
}
}
//ListEmail->Lines->Add(strTemp);
lpWABObject->FreeBuffer(lpRow);
}
lpWABObject->FreeBuffer(lpRows);
}
}
//FreeLibrary(hinstLib);(装载和释放 dll 文件的地方改成在主程序执行之前装载,结束的时候释放)
}

}
catch(...)
{
MessageBox(Handle,&quot;没有找到地址薄!&quot;,&quot;提示信息&quot;,MB_OK);
}
}
 
一下是我在网上看到的,但我试验了一下,无法运行,哪位运行成功了给我个Demo.
邮箱: bingmao@@163.com
CB6中读取OUTLOOK中的地址簿
作者: 来自: 阅读次数: 6 [大 中 小]
--------------------------------------------------------------------------------

首先感谢在这方面研究过的前辈们!
express 和 outlook都能读
cpp前面要声明
#pragma link &quot;Outlook_2K_SRVR&quot;
#pragma link &quot;IvDictio&quot;
#pragma link &quot;IvMulti&quot;
typedef HRESULT (WINAPI *fWABOpen)(LPADRBOOK*,LPWABOBJECT*,LPWAB_PARAM,DWORD);
用了bcb的outlook控件的
代码
try{
FormList->ListView1->Clear();
_ApplicationPtr pApp;
_NameSpace* pNS;
AddressLists* pAddr;
AddressEntries * pAddrEntries;
AddressEntry* pEntry;
AddressList* pList;
int count,count1;
pApp = this->OutlookApplication1->get_Application();
pNS = pApp->GetNamespace(WideString(&quot;MAPI&quot;));
pAddr = pNS->AddressLists;
count = pAddr->Count;
WideString email;
email.SetLength(128);
//emailname.SetLength(128);
for(int i = 1;i <= count;i++)
{
pList = pAddr->Item(TVariant(i));
pAddrEntries = pList->get_AddressEntries();
count1 = pAddrEntries->Count;
for(int j = 1;j <= count1;j++)
{
pEntry = pAddrEntries->Item(TVariant(j));
//pEntry->get_Name(&amp;emailname);
pEntry->get_Address(&amp;email);
//this->Memo1->Lines->Add(AnsiString(emailname.c_bstr()) + &quot;/t&quot;
+ AnsiString(email.c_bstr()));
TListItem * ListItem;
ListItem = FormList->ListView1->Items->Add();
ListItem->Caption = &quot;&quot;;
ListItem->SubItems->Add(AnsiString(email.c_bstr()));
ListItem->Checked = true;
}
}
ListView1->SetFocus();
ListView1->Update();
}
catch(...)
{
try{
HRESULT hRes;
//LPADRBOOK lpAdrBook;
DelphiInterface<IAddrBook> lpAdrBook;
//LPWABOBJECT lpWABObject;
DelphiInterface<IWABObject> lpWABObject;
//LPWAB_PARAM lpWABParam = NULL;
DWORD Reserved2 = NULL;
char szPath[MAX_PATH];
SHGetFolderPathX(szPath);
char * s = &quot;//System//wab32.dll&quot;;
strcat(szPath, s);
HINSTANCE hinstLib;
hinstLib = LoadLibrary(szPath);
// hinstLib = LoadLibrary(s.c_str()+&quot;//System//wab32.dll&quot;);
fWABOpen procWABOpen;
if (hinstLib != NULL)
{
// 获取&quot;Wab32.dll&quot;内部涵数WABOpen的进程地址
procWABOpen = (fWABOpen) GetProcAddress(hinstLib, &quot;WABOpen&quot;);
if (procWABOpen != NULL)
{
hRes = (procWABOpen)(&amp;lpAdrBook,&amp;lpWABObject,NULL,Reserved2);
//_ASSERTE(hRes == S_OK);
if (hRes != S_OK)
exit(1);
ULONG lpcbEntryID;
ENTRYID *lpEntryID;
hRes = lpAdrBook->GetPAB(
&amp;lpcbEntryID,
&amp;lpEntryID
);
//_ASSERTE(hRes == S_OK);
if (hRes != S_OK)
exit(2);
ULONG ulFlags = MAPI_BEST_ACCESS;
ULONG ulObjType = NULL;
//LPUNKNOWN lpUnk = NULL;
DelphiInterface<IABContainer> lpContainer;
hRes = lpAdrBook->OpenEntry(
lpcbEntryID,
lpEntryID,
NULL,
ulFlags,
&amp;ulObjType,
(LPUNKNOWN FAR *)&amp;lpContainer
);
ulFlags = NULL;
if (ulObjType == MAPI_ABCONT)
{
//IABContainer *lpContainer = static_cast <IABContainer *>(lpUnk);
LPMAPITABLE lpTable = NULL;
hRes = lpContainer->GetContentsTable(
ulFlags,
&amp;lpTable
);
//_ASSERT(lpTable);
ULONG ulRows;
hRes = lpTable->GetRowCount(0,&amp;ulRows);
//_ASSERTE(hRes == S_OK);
SRowSet *lpRows;
hRes = lpTable->QueryRows(
ulRows,// 获取所有行
0,
&amp;lpRows
);
ListView1->Clear();
for(ULONG i=0;i<lpRows->cRows;i++)
{
SRow *lpRow = &amp;lpRows->aRow;
AnsiString strTemp;
for(ULONG j=0;j<lpRow->cValues;j++)
{
SPropValue *lpProp = &amp;lpRow->lpProps[j];
if (lpProp->ulPropTag == PR_EMAIL_ADDRESS_A)
{
TListItem * ListItem;
ListItem = FormList->ListView1->Items->Add();
ListItem->Caption = &quot;&quot;;
ListItem->SubItems->Add((char *)lpProp->Value.lpszA);
//strTemp =(char *)lpProp->Value.lpszA;
ListItem->Checked = true;
}
}
//ListEmail->Lines->Add(strTemp);
lpWABObject->FreeBuffer(lpRow);
}
lpWABObject->FreeBuffer(lpRows);
}
}
//FreeLibrary(hinstLib);(装载和释放 dll 文件的地方改成在主程序执行之前装载,结束的时候释放)
}

}
catch(...)
{
MessageBox(Handle,&quot;没有找到地址薄!&quot;,&quot;提示信息&quot;,MB_OK);
}
}
 
补充一下:
Button1->Enabled = false;
try
{
Memo1->Clear();
_ApplicationPtr pApp;
_NameSpace* pNS;
AddressLists* pAddr;
AddressEntries * pAddrEntries;
AddressEntry* pEntry;
AddressList* pList;
int count,count1;
//Variant MyFolder, MyItem;
pApp = this->OutlookApplication1->get_Application();
pNS = pApp->GetNamespace(WideString(&quot;MAPI&quot;));
// MyFolder = pNS->GetDefaultFolder(olFolderContacts);
// for (int i=1;
i<(pAddr->Count);
i++ )
// {
// MyItem := pAddr.Items;
// }
//for i := 1 to MyFolder.Items.Countdo
begin
pAddr = pNS->AddressLists;
count = pAddr->Count;
WideString email,manager;
WideString lastName;
email.SetLength(128);
lastName.SetLength(128);
manager.SetLength(128);
for(int i = 1;i <= count;i++)
{
pList = pAddr->Item(TVariant(i));
pAddrEntries = pList->get_AddressEntries();
count1 = pAddrEntries->Count;
for(int j = 1;j <= count1;j++)
{
pEntry = pAddrEntries->Item(TVariant(j));
//pEntry->get_Name(&amp;emailname);
pEntry->get_Address(&amp;email);
//this->Memo1->Lines->Add(AnsiString(emailname.c_bstr()) + &quot;/t&quot;
+ AnsiString(email.c_bstr()));
pEntry->get_Name(&amp;lastName);
// pEntry->get_Manager(&amp;manager);

Memo1->Lines->Add(IntToStr(i) + AnsiString(email.c_bstr()) + AnsiString(lastName.c_bstr())
+ AnsiString(manager.c_bstr() ));
}
Memo1->Lines->Add(IntToStr(i));
}
}
catch(...)
{
}
Button1->Enabled = true;
可以获取名字和mail.但其他字段,正在研究中
 
后退
顶部