list and add contacts of outlook via VC
#include <iostream>
#include <tchar.h>
#import "G:\Program Files\Microsoft Office\OFFICE11\MSOUTL.OLB"
named_guids, raw_interfaces_only
#include <comdef.h>
#include <atlcomcli.h>
#include <comutil.h>
#pragma comment(lib,"comsupp.lib")
#include <iomanip>
struct InitOle {
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
HRESULT create_contact(LPDISPATCH pDisp,LPCTSTR name)
{
HRESULT hr = S_OK;
Outlook::_ContactItemPtr pContact;
if(pDisp)
hr =
pDisp->QueryInterface(__uuidof(Outlook::_ContactItem),(LPVOID*)&pConta
ct);
if(pContact)
{
hr = pContact->put_LastName(_bstr_t(name));
SYSTEMTIME st;
st.wYear = 1980;
st.wMonth = 1;
st.wDay = 1;
st.wHour = 1;
st.wMinute = 1;
st.wSecond = 1;
st.wMilliseconds = 0;
DATE dt;
SystemTimeToVariantTime(&st,&dt);
hr = pContact->put_Birthday(dt);
hr = pContact->Save();
}
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
//Attach to the running instance...
HRESULT hr = S_OK;
CLSID clsid;
LPCTSTR app_progid = _T("Outlook.Application");
CLSIDFromProgID(_bstr_t(app_progid), &clsid);
CComPtr<IUnknown> pUnk = NULL;
Outlook::_ApplicationPtr pApp;
for(int i=1;i<=5;i++) //try attaching for up to 5 attempts
{
HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
if(SUCCEEDED(hr))
{
pUnk->QueryInterface(__uuidof(Outlook::_Application),(void**)&pApp)
;
break;
}
::Sleep(1000);
}
if(NULL == pApp)
pApp.CreateInstance(app_progid);
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
Outlook::_NameSpacePtr pNameSpace;
if(pApp)
{
pApp->GetNamespace(_bstr_t("MAPI"),&pNameSpace);
}
if(pNameSpace)
pNameSpace->Logon(vtEmpty, vtEmpty, vtEmpty, vtEmpty);
Outlook::MAPIFolderPtr pFolder;
if(pNameSpace)
pNameSpace->GetDefaultFolder(Outlook::olFolderContacts,&pFolder);
long item_count = 0;
Outlook::_ItemsPtr pItems;
if(pFolder)
pFolder->get_Items(&pItems);
if(pItems)
pItems->get_Count(&item_count);
for(long index = 1; index <= item_count; index++)
{
CComPtr<IDispatch> pDisp = NULL;
pItems->Item(_variant_t(index), (LPDISPATCH*)&pDisp);
Outlook::_ContactItemPtr pContact;
pDisp->QueryInterface(__uuidof(Outlook::_ContactItem),(LPVOID*)&pCon
tact);
if(pContact)
{
_bstr_t bsname;
pContact->get_LastName(bsname.GetAddress());
std::cout<<"contact ["<<index<<"]";
if(bsname.length())
std::cout<<(LPCTSTR)bsname<<" ";
DATE dt = 0;
HRESULT hr = pContact->get_Birthday(&dt);
if(SUCCEEDED(hr))
{
SYSTEMTIME st;
VariantTimeToSystemTime(dt,&st);
std::cout<<" birthday:"<<
st.wYear<<"/"<<std::setw(2)<<std::setfill('0')<<st.wMonth<<"/"
<<std::setw(2)<<std::setfill('0')<<st.wDay;
}
std::cout<<std::endl;
}
}
//create contact in outlook2003 via VC2003 by Onega
if(pItems)
{
CComPtr<IDispatch> pDisp;
hr =
pItems->Add(_variant_t((long)Outlook::olContactItem,VT_I4),&pDisp);
hr = create_contact(pDisp,"by Items");
}
if(pApp)
{
CComPtr<IDispatch> pDisp;
hr = pApp->CreateItem(( Outlook::olContactItem),&pDisp);
hr = create_contact(pDisp,"by app");
}
system("pause");
return 0;
}
#include <tchar.h>
#import "G:\Program Files\Microsoft Office\OFFICE11\MSOUTL.OLB"
named_guids, raw_interfaces_only
#include <comdef.h>
#include <atlcomcli.h>
#include <comutil.h>
#pragma comment(lib,"comsupp.lib")
#include <iomanip>
struct InitOle {
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
HRESULT create_contact(LPDISPATCH pDisp,LPCTSTR name)
{
HRESULT hr = S_OK;
Outlook::_ContactItemPtr pContact;
if(pDisp)
hr =
pDisp->QueryInterface(__uuidof(Outlook::_ContactItem),(LPVOID*)&pConta
ct);
if(pContact)
{
hr = pContact->put_LastName(_bstr_t(name));
SYSTEMTIME st;
st.wYear = 1980;
st.wMonth = 1;
st.wDay = 1;
st.wHour = 1;
st.wMinute = 1;
st.wSecond = 1;
st.wMilliseconds = 0;
DATE dt;
SystemTimeToVariantTime(&st,&dt);
hr = pContact->put_Birthday(dt);
hr = pContact->Save();
}
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
//Attach to the running instance...
HRESULT hr = S_OK;
CLSID clsid;
LPCTSTR app_progid = _T("Outlook.Application");
CLSIDFromProgID(_bstr_t(app_progid), &clsid);
CComPtr<IUnknown> pUnk = NULL;
Outlook::_ApplicationPtr pApp;
for(int i=1;i<=5;i++) //try attaching for up to 5 attempts
{
HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
if(SUCCEEDED(hr))
{
pUnk->QueryInterface(__uuidof(Outlook::_Application),(void**)&pApp)
;
break;
}
::Sleep(1000);
}
if(NULL == pApp)
pApp.CreateInstance(app_progid);
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
Outlook::_NameSpacePtr pNameSpace;
if(pApp)
{
pApp->GetNamespace(_bstr_t("MAPI"),&pNameSpace);
}
if(pNameSpace)
pNameSpace->Logon(vtEmpty, vtEmpty, vtEmpty, vtEmpty);
Outlook::MAPIFolderPtr pFolder;
if(pNameSpace)
pNameSpace->GetDefaultFolder(Outlook::olFolderContacts,&pFolder);
long item_count = 0;
Outlook::_ItemsPtr pItems;
if(pFolder)
pFolder->get_Items(&pItems);
if(pItems)
pItems->get_Count(&item_count);
for(long index = 1; index <= item_count; index++)
{
CComPtr<IDispatch> pDisp = NULL;
pItems->Item(_variant_t(index), (LPDISPATCH*)&pDisp);
Outlook::_ContactItemPtr pContact;
pDisp->QueryInterface(__uuidof(Outlook::_ContactItem),(LPVOID*)&pCon
tact);
if(pContact)
{
_bstr_t bsname;
pContact->get_LastName(bsname.GetAddress());
std::cout<<"contact ["<<index<<"]";
if(bsname.length())
std::cout<<(LPCTSTR)bsname<<" ";
DATE dt = 0;
HRESULT hr = pContact->get_Birthday(&dt);
if(SUCCEEDED(hr))
{
SYSTEMTIME st;
VariantTimeToSystemTime(dt,&st);
std::cout<<" birthday:"<<
st.wYear<<"/"<<std::setw(2)<<std::setfill('0')<<st.wMonth<<"/"
<<std::setw(2)<<std::setfill('0')<<st.wDay;
}
std::cout<<std::endl;
}
}
//create contact in outlook2003 via VC2003 by Onega
if(pItems)
{
CComPtr<IDispatch> pDisp;
hr =
pItems->Add(_variant_t((long)Outlook::olContactItem,VT_I4),&pDisp);
hr = create_contact(pDisp,"by Items");
}
if(pApp)
{
CComPtr<IDispatch> pDisp;
hr = pApp->CreateItem(( Outlook::olContactItem),&pDisp);
hr = create_contact(pDisp,"by app");
}
system("pause");
return 0;
}
0 Comments:
Post a Comment
<< Home