Custom button and mutiple documents... Lost (completely)

  • Thread starter Vladimir Chtchetkine
  • Start date
V

Vladimir Chtchetkine

Hi everyone!

Here is a problem. I wrote a simple ATL COM add-in to MS
WORD that adds a toolbar+button to Word's commandbars and
now I want to:
1. Enable/disable it depending on a document state.
2. Handle "clicks" in appropriate "document" instance.
Here is how addin is structured:
1. In _IDTExtensibility2::OnConnection I hookup to
ApplicationEvents2 receiving "DocumentOpen"
and "NewDocument" events.
2. In _IDTExtensibility2::OnStartupComplete I add (if
doesn't exist) my custom toolbar+button to the app's
commandbar
3. In DocOpen and NewDoc handlers I create an object that
represents that particular document (lets say MyDoc) In
MyDoc initialization routine I find my custom button
(starting with _Document::GetControlbars()) and hookup it
to _CommandbarButtonEvents

So far so good. Button gets added, clicks are coming down
the pipe, button::Enable works like Japanese car's
engine, but.

But that's for a single doc environment! If I have two
docs opened button::Enable within one MyDoc's environment
enables or disables my button in another doc too. Same
thing with clicks. I'm receiving two of them, one in each
MyDoc instance although I pressed that button only once
and only in one document window.

Can anybody shed some light on what do I do wrong and how
to do it right?

TIA,

Vladimir

P.S. I'm new to Word add-ins so forgive me if I missed
something really obvious :)

P.P.S I'm sorry if it's a duplicate. I have had some
difficulties posting this first time...
 
E

Eugene E. Starostin

Hi Vladimir,
But that's for a single doc environment! If I have two
docs opened button::Enable within one MyDoc's environment
enables or disables my button in another doc too. Same
thing with clicks. I'm receiving two of them, one in each
MyDoc instance although I pressed that button only once
and only in one document window.


Ask our guys on Add-in Express forums:
http://www.add-in-express.com/forum/

They seem to fix these problems.

Eugene Starostin
Afalina Co., Ltd.
 
S

S.Vidyaraman

I think this might be a variation of the memory leak / garbage collector
problem. (http://blogs.msdn.com/bowerm/archive/2004/06/22/162304.aspx)

My guess is you are reusing the button variable for adding the event handler
rather than creating a new button variable for each new document created.
That's why the button behavior is the same across all documents that are
open.
Hope this helps.
S.Vidyaraman
 

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