Click here to Skip to main content
15,886,689 members
Articles / Desktop Programming / ATL

A VC++ Outlook COM Add-in that publicizes a custom form

Rate me:
Please Sign up or sign in to vote.
4.27/5 (7 votes)
6 Apr 20054 min read 183.3K   1.5K   42  
This article explains how to publicize and retrieve data from custom Outlook forms programmatically.
#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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
Italy Italy
See Andrea's profile on Linkedin

Comments and Discussions