Click here to Skip to main content
15,891,847 members
Articles / Desktop Programming / ATL

Writing an MS Word addin

Rate me:
Please Sign up or sign in to vote.
4.76/5 (19 votes)
13 Apr 20039 min read 396.9K   3.9K   89  
Writing a Word Addin using COM and VB Macros
////////////////////////////////////////////////////////////////////////////
//	Copyright : Amit Dey 2003
//
//	Email :visualcdev@hotmail.com
//
//	This code may be used in compiled form in any way you desire. This
//	file may be redistributed unmodified by any means PROVIDING it is 
//	not sold for profit without the authors written consent, and 
//	providing that this notice and the authors name is included.
//
//	This file is provided 'as is' with no expressed or implied warranty.
//	The author accepts no liability if it causes any damage to your computer.
//
//	Do expect bugs.
//	Please let me know of any bugs/mods/improvements.
//	and I will try to fix/incorporate them into this file.
//	Enjoy!
//
//	Description : Universal Word Addin
////////////////////////////////////////////////////////////////////////////
// Addin.cpp : Implementation of CAddin

#include "stdafx.h"
#include "UniversalAddin.h"
#include "Addin.h"

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

STDMETHODIMP CAddin::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IAddin
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CAddin::Init(IDispatch* pDisp)
{
	// TODO: Add your implementation code here
	CComQIPtr<MSWORD::_Application> spApp(pDisp);
	CComPtr <Office::CommandBars> spCmdBars; 
	ATLASSERT(spApp);
	HRESULT hr = spApp->get_CommandBars(&spCmdBars);
	if(FAILED(hr))
		return hr;

	HBITMAP	hBmp = ::LoadBitmap(_Module.GetResourceInstance(),MAKEINTRESOURCE(IDB_BUTTON));
	::OpenClipboard(NULL);
	::EmptyClipboard();
	::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
	::CloseClipboard();
	::DeleteObject(hBmp);


	CComVariant vtName(_T("Universal Addin"));
	CComVariant vtPos(1); // position it below all toolbands
	CComVariant vtTemp(VARIANT_TRUE); // is temporary		
	CComVariant vtEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
	CComPtr < Office::CommandBar> spNewCmdBar;
	spNewCmdBar = spCmdBars->Add( vtName,vtPos, vtEmpty, vtTemp);
	ATLASSERT(spNewCmdBar);

	CComPtr < Office::CommandBarControls> spBarControls;
	spBarControls = spNewCmdBar->GetControls();
	ATLASSERT(spBarControls);

	CComVariant vtToolBarType(1);
	CComPtr < Office::CommandBarControl> spNewBar; 
	CComVariant vtShow(VARIANT_TRUE);	
	// add  button
	spNewBar = spBarControls->Add(vtToolBarType, vtEmpty, vtEmpty, vtEmpty, vtShow); 
	ATLASSERT(spNewBar);
	
	_bstr_t bstrNewCaption(OLESTR("Universal Addin"));
	_bstr_t bstrTipText(OLESTR("Universal Addin"));
			
	// get CommandBarButton interface so we can specify button styles and stuff
	// each button displays a bitmap and caption next to it
	CComQIPtr < Office::CommandBarButton> spCmdButton(spNewBar);
	ATLASSERT(spCmdButton);
	
	spCmdButton->PutStyle(Office::msoButtonIconAndCaption);
	spCmdButton->PasteFace();  // use own bitmap
	spCmdButton->PutVisible(VARIANT_TRUE); 
	spCmdButton->PutCaption(OLESTR("Button")); 
	spCmdButton->PutEnabled(VARIANT_TRUE);
	spCmdButton->PutTooltipText(OLESTR("Tooltip for Button")); 
	spCmdButton->PutTag(OLESTR("Button"));
	spCmdButton->PutCaption(_T("Button")); 
	spCmdButton->put_OnAction(OLESTR("ClickButton1")); 
	spCmdButton->PutVisible(VARIANT_TRUE);
	spNewCmdBar->PutVisible(VARIANT_TRUE); 		

	::EmptyClipboard();
	return S_OK;
}

STDMETHODIMP CAddin::Uninit(IDispatch* pDisp)
{
	// TODO: Add your implementation code here
	CComQIPtr<MSWORD::_Application> spApp(pDisp);
	ATLASSERT(spApp);
   	CComPtr < Office::CommandBars> spCmdBars; 
	CComPtr < Office::CommandBar> spCmdBar;		
	spApp->get_CommandBars(&spCmdBars);

	m_spApp = NULL;
	return S_OK;
}

STDMETHODIMP CAddin::ClickItem1()
{
	// TODO: Add your implementation code here
	ATLTRACE("\nClickItem1");
	MessageBox(NULL,_T("ClickItem1"),_T("UniversalAddin"),MB_OK);
	return S_OK;
}

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
Web Developer
India India
Amit Dey is a freelance programmer from Bangalore,India. Chiefly programming VC++/MFC, ATL/COM and PocketPC and Palm platforms. Apart from programming and CP, he is a self-taught guitar and keyboard player.

He can be contacted at visualcdev@hotmail.com


Comments and Discussions