Can't get Word::IApplicationEvents2 to fire

B

bubbans_2

I have a dialog based MFC AppWizard application in which I'm attemptin
to wire up a Word::IApplicationEvents2 handler. I had no proble
wiring up a Word::IApplicationEvents handler, but for some reason th
IApplicationEvents2 handler reactes strangely. The event handle
methods for only 3 of the events get called: Startup, Quit
DocumentChange. I implemented a handler for the DocumentOpen, but i
never gets called. I've seen 1 other person complain of this exac
same problem, but it look like he never got an answer. Does anyon
have any ideas as to what is wrong here?

<code>
Word::_ApplicationPtr spWord;

OleInitialize(NULL);

CoInitialize(NULL);

HRESULT hr = spWord.CreateInstance(__uuidof(Word::Application));
ASSERT(SUCCEEDED(hr));

spWord->put_Visible(VARIANT_TRUE);
spWord->Activate();
Sleep(3000);

static const GUID IID_IWordAppEvents2 =
{0x000209fe,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46
};

// Steps for setting up events.
// 1. Get server's IConnectionPointContainer interface.
// 2. Call IConnectionPointContainerFindConnectionPoint()
// to find the event you want to catch.
// 3. Call IConnectionPoint::Advise() with the IUnknown
// interface of your implementation of the events.
// Get server's IConnectionPointContainer interface.
IConnectionPointContainer *pConnPtContainer;
hr = spWord->QueryInterface(IID_IConnectionPointContainer,
(void **)&pConnPtContainer);
ASSERT(!FAILED(hr));

// Find connection point for events you're interested in.
hr = pConnPtContainer->FindConnectionPoint(IID_IWordAppEvents2,
&m_pConnectionPoint);
ASSERT(!FAILED(hr));

// Get the IUnknown interface of your event implementation.
LPUNKNOWN pUnk = m_myEventSink.GetInterface(&IID_IUnknown);
ASSERT(pUnk);

// Setup advisory connection!
hr = m_pConnectionPoint->Advise(pUnk, &m_adviseCookie);
ASSERT(!FAILED(hr));

// Release IConnectionPointContainer interface.
pConnPtContainer->Release();
</code>

<code>
// MyEventSink.cpp : implementation file
//

#include "stdafx.h"
#include "WordEvents.h"
#include "MyEventSink.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MyEventSink

IMPLEMENT_DYNCREATE(MyEventSink, CCmdTarget)

MyEventSink::MyEventSink()
{
EnableAutomation();
}

MyEventSink::~MyEventSink()
{
}


void MyEventSink::OnFinalRelease()
{
// When the last reference for an automation object is released
// OnFinalRelease is called. The base class will automatically
// deletes the object. Add additional cleanup required for your
// object before calling the base class.

CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(MyEventSink, CCmdTarget)
//{{AFX_MSG_MAP(MyEventSink)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(MyEventSink, CCmdTarget)
//{{AFX_DISPATCH_MAP(MyEventSink)
DISP_FUNCTION_ID(MyEventSink, "Startup", 1, Startup, VT_EMPTY
VTS_NONE)
DISP_FUNCTION_ID(MyEventSink, "Quit", 2, Quit, VT_EMPTY, VTS_NONE)
DISP_FUNCTION_ID(MyEventSink, "DocumentChange", 3, DocumentChange
VT_EMPTY, VTS_NONE)
DISP_FUNCTION_ID(MyEventSink, "DocumentOpen", 4, DocumentOpen
VT_EMPTY, VTS_DISPATCH)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IMyEventSink to support typesaf
binding
// from VBA. This IID must match the GUID that is attached to the
// dispinterface in the .ODL file.

// {66DE9AD0-44B3-43A5-BC77-00EE0574C134}
static const IID IID_IMyEventSink =
{0x000209fe,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46
};
//__uuidof(Word::ApplicationEvents2);

BEGIN_INTERFACE_MAP(MyEventSink, CCmdTarget)
INTERFACE_PART(MyEventSink, IID_IMyEventSink, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyEventSink message handlers

void MyEventSink::Startup()
{
// TODO: Add your dispatch handler code here
AfxMessageBox("Startup");
}

void MyEventSink::Quit()
{
// TODO: Add your dispatch handler code here
AfxMessageBox("Quit");
}

void MyEventSink::DocumentChange()
{
// TODO: Add your dispatch handler code here
AfxMessageBox("DocumentChange!!!");
}

void MyEventSink::DocumentOpen(LPDISPATCH pDispatch)
{
// TODO: Add your dispatch handler code here
AfxMessageBox("DocumentOpen");
}

</code>
 
B

Bob Buckland ?:-\)

Hi Bubbans_2,

You may want to use the link below to post this in one of the Word/Office programming groups.

==========

I have a dialog based MFC AppWizard application in which I'm attempting
to wire up a Word::IApplicationEvents2 handler. I had no problem
wiring up a Word::IApplicationEvents handler, but for some reason the
IApplicationEvents2 handler reactes strangely. The event handler
methods for only 3 of the events get called: Startup, Quit,
DocumentChange. I implemented a handler for the DocumentOpen, but it
never gets called. I've seen 1 other person complain of this exact
same problem, but it look like he never got an answer. Does anyone
have any ideas as to what is wrong here? >>
--
Please let us know if this has helped,

Bob Buckland ?:)
MS Office System Products MVP

LINKS
A. Specific newsgroup/discussion group mentioned in this message:
news://msnews.microsoft.com/microsoft.public.officedev
or via browser:
http://microsoft.com/communities/newsgroups/en-us/?dg=microsoft.public.officedev

B. MS Office Community discussion/newsgroups via Web Browser
http://microsoft.com/office/community/en-us/default.mspx
or
Microsoft hosted newsgroups via Outlook Express/newsreader
news://msnews.microsoft.com
 
B

bubbans_2

Ok. I submitted the same question at the link you supplied.
I'll see if I get any responses there. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top