COM add-in stops responding to command button clicks?

G

Gary McGill

Hi,

I'm trying to develop a COM add-in using C# and VS.NET 2003. I'm doing
development under Windows XP and the version of Office on my development
machine is also XP. (Eventually I would like my add-in to work with all
Office versions from 2000 up, but looking over this group it looks like
that'll be a challenge in itself).

Anyway... the problem. I've followed the steps described in the (scant)
documentation available (KB 302901), and have an add-in that adds a new
command button to Word. Actually I've modified it somewhat, so I'm adding my
own command bar but I doubt that's relevant.

It all works fine if I start off with a blank document and click a button on
my command bar - my dialog comes up, and my code adds a new field to the
document. BUT, once I've done this once, all the buttons on my command bar
stop working.

What's even stranger is that if I save my document, re-start Word and open
the document again, the mere fact that I've opened this (contaminated?)
document means that my command bar buttons don't work. Once they stop
working they never start again, no matter what document I have open.

If on the other hand I start Word and DON'T open a contaminated document,
the command bar works fine. It looks like I've created a command bar-killing
document!

At one point I saw Dave's "tech note" about keeping a reference to menu
items you add from your COM add-in so that they don't get GC'd, and figured
maybe that was my problem. But when I added references to my command bar and
to the buttons (and even the event handlers, for good measure) it made no
difference.

I've now spent about 50% of my total development time to date on trying to
get these COM add-in things to work. Most of *that* time was spent trying to
find some decent documentation - and were it not for the "third party"
documentation (thanks, Dave) I'd have given up long before now.

I'll stop now before I rant myself into a lather. Any help gratefully
received.

Gary
 
G

Gary McGill

Hmmm... just been playing around with this some more, and it turns out it's
not actually to do with the specific document being opened - it's to do with
how many documents are open.

When I start Word, it comes up with a blank document and the buttons work.

If I close that document and open another document, the buttons still work
(even my "killer" document").

However, if at any time I open a second document (which in Word XP opens in
a new application window), the buttons don't work on the new document.
However, they still work on the first document.

So, it looks like the buttons only work for the first Word application
window, and not on any subsequent windows.

I know that my "connect" code is only being run when Word first starts up
(this behaviour is specified in a registry key somewhere, I think), so
although there's a command bar shown on each Word application window, these
must be representations of the SAME command bar? Or does Word re-create a
separate copy of the command bar for each application window? Either way, I
guess it's not hooking the second command bar up to my event handlers...

Is there a way around this?
 
K

Ken Slovak - [MVP - Outlook]

You would need to add your buttons to each document, perhaps handling the
NewDocument event. Also, make sure to have a unique Tag property for each
button you add so only that button fires when it is clicked and not all the
clone buttons in other document windows.

In Outlook we use a wrapper class that encapsulates all the events of
whatever object we want to handle, in this case it would be Word.Document
events. Then we add each class to a collection so it remains in scope while
the object underlying the class has life. In the class we create the buttons
with a unique Tag and handle the button Click event in that class. That way
you get discrete click events and know which document you are responding to.
 
G

Gary McGill

Ken,

Thanks for your reply. Funnily enough, I came across it on Google as I was
searching for other posts on the same subject, having just checked this
newsgroup to see if anyone had replied (which at that point you hadn't).
Google is fast! :)

While I was doing my Googling, I came across another similar post + reply on
the VB.NET newsgroup that mentioned using a unique tag. I just tried that,
and it works fine. Now I feel stupid for removing the tag-setting stuff from
the sample code that I started with (although I wouldn't have done that if
the associated comment had said "you need this otherwise your buttons won't
work" :))

Do I really have to do the other stuff you mention (adding buttons to each
document, etc.)? Given that my buttons now work across documents, it doesn't
seem necessary - but I don't want to make the mistake of ignoring good
advice twice...

FYI I'm only targetting Word, Excel & PowerPoint - no Outlook.

Thanks again,
Gary
 
T

Tom Winter

I haven't done .NET work (I'm still in VB6-land), but in any case, I would
not recommend adding buttons to each document. In Word, menu/toolbar
customizations are made to TEMPLATES anyway, so the odds are you're always
modifying normal.dot (which really isn't a good idea either, but that's a
topic for another day....). Outlook uses an entirely different model for
windows/menus/toolbar. What Ken said is true for Outlook, but not Word.
 
G

Gary McGill

OK, thanks.

Tom Winter said:
I haven't done .NET work (I'm still in VB6-land), but in any case, I would
not recommend adding buttons to each document. In Word, menu/toolbar
customizations are made to TEMPLATES anyway, so the odds are you're always
modifying normal.dot (which really isn't a good idea either, but that's a
topic for another day....). Outlook uses an entirely different model for
windows/menus/toolbar. What Ken said is true for Outlook, but not Word.
--
Tom Winter
(e-mail address removed)
www.AmosFiveSix.com

reply starts re-create
 
K

Ken Slovak - [MVP - Outlook]

Tom is correct. The wrapper type-stuff wouldn't apply to Word, as I
mentioned that's what we do in Outlook. Just make sure you use unique Tag
properties for your buttons.

Also, you are modifying Normal.dot usually when you add buttons. They will
become permanent if the changes to Normal.dot are saved, either by the user
or automatically. I usually check for Application.CustomizationContext.Saved
before I add and delete my Word buttons. If True then Normal.dot was
customized. I preserve that and if I delete a button and Word wasn't
customized I set CustomizationContext.Saved = True after deleting the button
so the user won't get prompted to save and Word won't save automatically on
exit.
 

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