Hey everybody, I put a post out about my word automation not
working. I haven't been able to get past the Link Error so I tried
something different. Now, I have this function which puts a button
(with a bitmap) onto MS Word. As you can see, I create it
dynamically. Now, what I was wondering, how can I capture the event
when this button is pressed? I am trying to do an Advise but it
doesn't seem to be doing what I want it to do??? I have pasted the
header file for the event sink and the actual function which does the
Advise. Any ideas? Anything come to mind? Do you think I am making
this too complicated and have a better way to go at it? Just let me
know, I am knew to COM/ATL/OLE development...

==============================================
header file wordeventsink.h:

#if !defined
(AFX_WORDEVENTSINK_H__F45A8330_C1E9_11D2_A0C4_0080 C7F3B56B__INCLUDED_)
#define
AFX_WORDEVENTSINK_H__F45A8330_C1E9_11D2_A0C4_0080C 7F3B56B__INCLUDED_

/*----------------------------------------------------------------------------*/

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/*----------------------------------------------------------------------------*/

//#pragma warning (disable:4146)
//#import "mso97.dll"
//#pragma warning (default:4146)
//#import "vbeext1.olb"
//#import "msword8.olb" rename("ExitWindows", "WordExitWindows")
#pragma warning (disable:4146)
#import "C:\Program Files\Common Files\Microsoft Shared\Office10
\mso.dll"
#pragma warning (default:4146)
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6
\vbe6ext.olb"
#import "C:\Program Files\Microsoft Office\Office10\MSWORD.olb" rename
("ExitWindows", "WordExitWindows")

#include "ConnectionAdvisor.h"
class CTestDlg;

/*----------------------------------------------------------------------------*/

const IID IID_IWordAppEventSink = __uuidof(Word::ApplicationEvents);
const IID IID_IWordDocEventSink = __uuidof(Word:ocumentEvents);
const IID IID_IPSISaveExit = __uuidof
(Office::_CommandBarButtonEvents);

/*----------------------------------------------------------------------------*/

class CWordEventSink : public CCmdTarget
{
DECLARE_DYNCREATE(CWordEventSink)

public:
CWordEventSink();
virtual ~CWordEventSink();
BOOL Advise(IUnknown* pSource, REFIID iid);
BOOL Unadvise(REFIID iid);
void SetLauncher(CTestDlg* pWordLauncher);

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWordEventSink)
public:
virtual void OnFinalRelease();
//}}AFX_VIRTUAL

protected:

// Generated message map functions
//{{AFX_MSG(CWordEventSink)
// NOTE - the ClassWizard will add and remove member
functions here.
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
// Generated OLE dispatch map functions
//{{AFX_DISPATCH(CWordEventSink)
afx_msg void OnAppStartup();
afx_msg void OnAppQuit();
afx_msg void OnAppDocumentChange();
afx_msg void OnDocNew();
afx_msg void OnDocOpen();
afx_msg void OnDocClose();
afx_msg void PSISaveAndExit();
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()

private:
CConnectionAdvisor m_AppEventsAdvisor;
CConnectionAdvisor m_DocEventsAdvisor;
CConnectionAdvisor m_BarEventsAdvisor;
CTestDlg* m_pWordLauncher;
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
immediately before the previous line.

#endif // !defined
(AFX_WORDEVENTSINK_H__F45A8330_C1E9_11D2_A0C4_0080 C7F3B56B__INCLUDED_)

==============================================
function in testdlg.cpp:

void CTestDlg::OnOK()
{
try
{
HRESULT hr = m_pWord.CreateInstance(__uuidof
(Word::Application));
ASSERT(SUCCEEDED(hr));

m_pDoc = m_pWord->Documents->Add();

BOOL Res = m_WordEventSink.Advise(m_pWord,
IID_IWordAppEventSink);
ASSERT(Res == TRUE);

Res = m_WordEventSink.Advise(m_pDoc,
IID_IWordDocEventSink);
ASSERT(Res == TRUE);

m_pWord->Visible = VARIANT_TRUE;

COleVariant covTrue((short) TRUE),
covFalse((short) FALSE),
covOptional((long)
DISP_E_PARAMNOTFOUND, VT_ERROR);


Office::_CommandBarsPtr oBars = m_pWord-
>GetCommandBars();
// Office::_CommandBarsPtr oBars;
// m_pWord->get_CommandBars(&oBars); // New CommandBars

VARIANT vName;
vName.vt = VT_BSTR;
vName.bstrVal = SysAllocString(L"PSI");

VARIANT vPosition;
vPosition.vt = VT_I2;
vPosition.iVal = 1; // 4 = Floating; 0 = top;

VARIANT vTemp;
vTemp.vt = VT_BOOL;
vTemp.boolVal = VARIANT_TRUE;

// Add new bar to the command bars collection
Office::CommandBarPtr oNewBar = oBars->Add(vName,
vPosition, covOptional, vTemp);

// oBars->raw_Add(vName, // const Variant Name
// vPosition, // const Variant
Position
// vTemp, // const Variant
(replace)MenuBar
// vTemp, // const Variant
Temporary
// &oNewBar
// );
//
oNewBar->Visible = VARIANT_TRUE;
// oNewBar->put_Visible(TRUE);

// Get the collection (now empty) of controls on the
new commandbar
Office::CommandBarControlsPtr oNewControls = oNewBar-
>GetControls();
// oNewBar->get_Controls(&oNewControls);

VARIANT vType;
vType.vt = VT_I4;
vType.lVal = (long)1;

Office::CommandBarControlPtr oNewControl =
oNewControls->Add(vType, covOptional, covOptional, covOptional,
vTemp);
// Office::_CommandBarButtonPtr m_pButton; // Office XP -
note leading underscore
// oNewControls->raw_Add(vType, // Type =
msoControlButton
// covOptional, // Id
// covOptional, // Parameter
// covOptional, // Before
// covTrue, // Temporary
// &oNewControl
// );
//
Office::_CommandBarButtonPtr oNewButton (oNewControl);
m_pButton = oNewButton;

Res = m_WordEventSink.Advise(m_pButton,
IID_IPSISaveExit);
ASSERT(Res == TRUE);

// m_pButton->Style(3);
// m_pButton->Caption(SysAllocString(L"PSI Save &
Exit"));
m_pButton->Visible = VARIANT_TRUE;
m_pButton->Enabled = TRUE;
m_pButton->
// m_pButton->put_Style
(Office::msoButtonIconAndCaption); // msoButtonIconAndCaption
// m_pButton->put_Caption(OLESTR("PSI Save & Exit"));
// m_pButton->put_TooltipText(OLESTR("This is a new
button"));
// m_pButton->put_Visible(VARIANT_TRUE);
// m_pButton->put_Enabled(VARIANT_TRUE);

OpenClipboard(); // Reserve clipboard for this program

CBitmap MyBitmap;
MyBitmap.LoadBitmap(IDB_BITMAP1); // A Bitmap you
drew in the
// Resource Editor
HBITMAP MyBitmapHandle = (HBITMAP)MyBitmap; // Cast
it to a HBITMAP
SetClipboardData(CF_BITMAP, MyBitmapHandle);
CloseClipboard(); // Free clipboard so PasteFace()
can use it

m_pButton->PasteFace();
GetDlgItem(IDOK)->EnableWindow(FALSE);
}
catch (_com_error& ComError)
{
DumpComError(ComError);
}
}