|
#include "stdafx.h"
#include ".\formtemplate.h"
FormTemplate* FormTemplate::_instance = 0;
FormTemplate* FormTemplate::GetInstance(CComPtr<Outlook::_Application> spApp) {
if (_instance == 0) {
_instance = new FormTemplate(spApp);
}
return _instance;
}
FormTemplate::~FormTemplate(void)
{
}
int FormTemplate::LoadCustomForm(void)
{
HRESULT hr;
CComQIPtr <Outlook::_ContactItem> CustomContactItem;
CComQIPtr <Outlook::_ContactItem> NewContactItem;
CComPtr <Outlook::FormDescription> CustomFormDescription;
CComVariant myFolder(DISP_E_PARAMNOTFOUND, VT_ERROR);
_bstr_t bstrName_T (_T("MAPI"));
BSTR bstrName;
bstrName = bstrName_T.copy();
CComPtr <Outlook::_NameSpace> olNs;
m_spApp->GetNamespace(bstrName,&olNs);
CComQIPtr <Outlook::MAPIFolder> oContacts;
Outlook::OlDefaultFolders enumODF = olFolderContacts;
hr = olNs->GetDefaultFolder(enumODF,&oContacts);
//change this to your Custom.oft path
bstrName_T = "C:\\Custom.oft";
bstrName = bstrName_T.copy();
hr = m_spApp->CreateItemFromTemplate(bstrName,myFolder,(IDispatch**) &CustomContactItem);
if(FAILED(hr)) {
string msg=(string)"Impossible to load "+ToStdString(bstrName);
MessageBox(NULL,msg.c_str(),"Outlook Addin",MB_ICONERROR);
return -1;
}
Outlook::OlInspectorClose oic;
CComVariant varContacts (oContacts);
//**** publish form in Contacts folder ****
bstrName_T = _T("Custom");
bstrName = bstrName_T.copy();
Outlook::OlFormRegistry ofr = olFolderRegistry;
hr = CustomContactItem->get_FormDescription(&CustomFormDescription);
if (FAILED(hr)) {
MessageBox(NULL,"Nothing form description","Outlook Addin",MB_ICONERROR);
return -1;
}
hr = CustomFormDescription->put_Name(bstrName);
hr = CustomFormDescription->PublishForm(ofr, varContacts);
if (FAILED(hr)) {
MessageBox(NULL,"Publish failed","Outlook Addin",MB_ICONERROR);
return -1;
}
CComQIPtr <Outlook::_Items> spItems;
oContacts->get_Items(&spItems);
BSTR oldMsgClass,newMsgClass;
_bstr_t oldMsgClass_T (_T(ADDIN_OLDMSGCLASS));
oldMsgClass = oldMsgClass_T.copy();
_bstr_t newMsgClass_T (_T(ADDIN_NEWMSGCLASS));
newMsgClass = newMsgClass_T.copy();
long ItemCount;
spItems->get_Count(&ItemCount);
int changes =0;
oic = olSave;
for (int n = 1; n<=ItemCount;++n) {
CComVariant nItem (n);
IDispatch* itm;
hr = spItems->Item(nItem,&itm);
if (FAILED(hr)) {
MessageBox(NULL,"Conversion error","Outlook Addin",MB_ICONERROR);
return -1;
}
CComQIPtr <Outlook::_ContactItem> spItem;
hr = itm->QueryInterface(&spItem);
if (FAILED(hr)) {
continue; //IPM.DistList
}
BSTR curMsgClass;
spItem->get_MessageClass(&curMsgClass);
if (FAILED(hr)) {
MessageBox(NULL,"Conversion error","Outlook Addin",MB_ICONERROR);
return -1;
}
string curMsgClassStr = ToStdString(curMsgClass);
string oldMsgClassStr = ToStdString(oldMsgClass);
if (curMsgClassStr.compare(oldMsgClassStr) ==0) {
spItem->put_MessageClass(newMsgClass);
spItem->Save();
spItem->Copy((IDispatch**)&NewContactItem);
NewContactItem->Close(oic);
spItem->Delete();
changes++;
}
}
oic = olDiscard;
CustomContactItem->Close(oic);
MessageBox(NULL,"Custom form loaded correctly","Outlook Addin",MB_ICONINFORMATION);
return 0;
}
int FormTemplate::LoadDefaultForm(void)
{
_bstr_t bstrName_T (_T("MAPI"));
BSTR bstrName;
bstrName = bstrName_T.copy();
CComPtr <Outlook::_NameSpace> olNs;
m_spApp->GetNamespace(bstrName,&olNs);
CComQIPtr <Outlook::MAPIFolder> oContacts;
Outlook::OlDefaultFolders enumODF = olFolderContacts;
olNs->GetDefaultFolder(enumODF,&oContacts);
BSTR defaultMsgClass;
_bstr_t defaultMsgClass_T (_T(ADDIN_OLDMSGCLASS));
defaultMsgClass = defaultMsgClass_T.copy();
CComQIPtr <Outlook::_Items> spItems;
oContacts->get_Items(&spItems);
long ItemCount;
spItems->get_Count(&ItemCount);
IDispatch* pItem,*pItemCopy;
CComQIPtr<Outlook::_ContactItem> pContact;
spItems->GetFirst(&pItem);
for (int n = 1; n<=ItemCount;n++) {
HRESULT hr=pItem->QueryInterface(&pContact);
if (FAILED(hr)) {
HRESULT nres = spItems->GetNext(&pItem);
if (FAILED (nres))
break;
continue; //IPM.DistList
}
pContact->put_MessageClass(defaultMsgClass);
pContact->Save();
pContact->Copy(&pItemCopy);
pContact->Delete();
OlInspectorClose oic = olSave;
pContact->Close(oic);
HRESULT nres = spItems->GetNext(&pItem);
if (FAILED (nres))
break;
}
MessageBox(NULL,"Default form loaded correctly","Outlook Addin",MB_ICONINFORMATION);
return 0;
}
std::string FormTemplate::ToStdString(BSTR bsData) const
{
_bstr_t bstrText(bsData);
static char chartext[255];
if (bstrText.length()>255)
return "";
sprintf(chartext,"%s",(LPCTSTR)bstrText);
std::string str (chartext);
return str;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.