Click here to Skip to main content
15,886,519 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.
// Addin.cpp : Implementation of CAddin
#include "stdafx.h"
#include "OutlookAddin.h"
#include "Addin.h"
#include ".\addin.h"

/////////////////////////////////////////////////////////////////////////////
// CAddin

_ATL_FUNC_INFO OnClickButtonInfo ={CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};
_ATL_FUNC_INFO OnOptionsAddPagesInfo = {CC_STDCALL,VT_EMPTY,1,{VT_DISPATCH}};


//visualizza il contact item alla page form custom
void __stdcall OutlookAddin::OnClickButton(IDispatch* Ctrl,VARIANT_BOOL * CancelDefault)
{
	_bstr_t bstrFullName = m_spButton->GetCaption();
	CComQIPtr <Outlook::_ContactItem> pContactItem;
	CComQIPtr <Outlook::_Inspector> pInspector;

	LPDISPATCH pContact = GetSelectedItem();
	HRESULT hr = pContact->QueryInterface(&pContactItem);
	if (FAILED(hr)) {
		MessageBox(NULL,"Select a contact item, please","Outlook Addin",MB_ICONERROR);
		return;
	}

	pContactItem->Display();
	m_spApp->ActiveInspector(&pInspector);
	BSTR pageName;
	_bstr_t pageStr ("Custom");
	pageName = pageStr.copy();
	pInspector->SetCurrentFormPage(pageName);
		_bstr_t empty = _T("");
	_bstr_t label = _T("Nickname");
	CComPtr <Outlook::UserProperties>pUserProperties;
	CComPtr <Outlook::UserProperty> pUserProperty;
	CComVariant value(DISP_E_PARAMNOTFOUND, VT_ERROR);
	if (!pContactItem)
		return;
	BSTR curMsgClass;
	hr = pContactItem->get_MessageClass(&curMsgClass);
	if (FAILED(hr))
		return;
	if (ToStdString(curMsgClass) != "IPM.Contact.Custom")
		return;

	hr = pContactItem->get_UserProperties(&pUserProperties);
	if (FAILED(hr)) 
		return;
	BSTR nameProp = label.copy();
	BSTR bstr_empty=empty.copy();

	hr = pUserProperties->Find(nameProp,value,&pUserProperty);
	if (FAILED(hr)) 
		return;

	pUserProperty->get_Value(&value);
	AfxMessageBox(ToStdString(value.bstrVal).c_str());
}

 

CComPtr < Office::_CommandBarButton> OutlookAddin::AddIconButton(const std::string& caption, const std::string& tooltip,
					const int& imageId, bool transparence, const VARIANT_BOOL& vBeginGroup,
					const CComPtr < Office::CommandBarControls>& pBarControls)
{
	CComVariant vToolBarType(1);
	CComVariant vShow(VARIANT_TRUE);
	CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);	

	CComPtr < Office::CommandBarControl> spNewBar; 
	// add button
	spNewBar = pBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow); 
	ATLASSERT(spNewBar);

	spNewBar->put_BeginGroup(vBeginGroup);

	// get CommandBarButton interface for each toolbar button
	// so we can specify button styles and stuff
	// each button displays a bitmap and caption next to it
	CComQIPtr < Office::_CommandBarButton> spButton(spNewBar);

	ATLASSERT(spButton);

	UINT colorMap;
	if (transparence)
		colorMap = LR_LOADMAP3DCOLORS|LR_LOADTRANSPARENT;
	else
		colorMap = LR_LOADMAP3DCOLORS;

	// to set a bitmap to a button, load a 32x32 bitmap
	// and copy it to clipboard. Call CommandBarButton's PasteFace()
	// to copy the bitmap to the button face. to use
	// Outlook's set of predefined bitmap, set button's FaceId to 	//the
	// button whose bitmap you want to use
	HBITMAP hBmp =(HBITMAP)::LoadImage(_Module.GetResourceInstance(),
	MAKEINTRESOURCE(imageId),IMAGE_BITMAP,0,0,colorMap);

	// put bitmap into Clipboard
	::OpenClipboard(NULL);
	::EmptyClipboard();
	::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
	::CloseClipboard();
	::DeleteObject(hBmp);

	// set style before setting bitmap
	spButton->PutStyle(Office::msoButtonIconAndCaption);

	HRESULT hr = spButton->PasteFace();
	if (FAILED(hr))
		return hr;

	std::string tag = "Tag "+caption; 
	spButton->PutVisible(VARIANT_TRUE); 
	if (caption.length() > 1) 
		spButton->PutCaption(caption.c_str()); 
	spButton->PutEnabled(VARIANT_TRUE);
	spButton->PutTooltipText(tooltip.c_str()); 
	spButton->PutTag(tag.c_str()); 

	return spButton;
}



void __stdcall OutlookAddin::OnOptionsAddPages(IDispatch* Ctrl)
{
  CComQIPtr<Outlook::PropertyPages> spPages(Ctrl);
  ATLASSERT(spPages);

  //ProgId of the propertypage control
  CComVariant varProgId(OLESTR("OutlookAddin.PropPage"));

  //tab text
  CComBSTR bstrTitle(OLESTR("Addin"));

  HRESULT hr = spPages->Add((_variant_t)varProgId,
                               (_bstr_t)bstrTitle);
	
  if(FAILED(hr))
    ATLTRACE("\nFailed adding propertypage");

}

LPDISPATCH OutlookAddin::GetSelectedItem() const
{
	HRESULT hr;
	LPDISPATCH olSelectedItem = NULL;	// Since you don't know what type to expect, just use an IDispatch*

	COleVariant covOptional((long) DISP_E_PARAMNOTFOUND, VT_ERROR);
 
	CComPtr<Outlook::_Explorer> spExplorer; 
	CComPtr<Outlook::Selection> olSel; 

	m_spApp->ActiveExplorer(&spExplorer);
	// Get the Selection object
	hr = spExplorer->get_Selection(&olSel);
	if (FAILED(hr))
		return NULL;

	// Since more than one object can be selected at any one time, you loop through them
	// one at a time
	long count;
	olSel->get_Count(&count);

	for (long i = 1; i <= count; ++i){
		COleVariant covIndex;
		covIndex.vt = VT_I4;
		covIndex.lVal = i;
		olSel->Item(covIndex,&olSelectedItem);	// Get the selected item
	}
	return olSelectedItem;
}


std::string OutlookAddin::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