Help! My CommandBarButton events stop firing



This is perhaps the most frequently asked question I hear when writing managed COM Addins or Smart Docs.  You’ve wired up an event handler to catch button clicks of custom menu items.  They work for the first few clicks but then mysteriously stop working.  What’s going on?


The answer is almost always that the CommandBarButton objects have been scoped at the method level.  What typically happens is that some indeterminate time after these objects go out of scope, the garbage collector runs and cleans up these objects along with the event hooks you have wired into.


The solution is to make sure you scope these objects as class-level variables so that they stick around until the class gets destroyed.

Comments (8)

  1. S.Vidyaraman says:

    Thanks ! That helps a lot. I have one other related isssue. Please let me know if there is a better place to ask these questions …… ( I already tried the NewGroups )

    If I wanted to tag an additional event to ALL the CommandBarButtons (say in MS Word), is it OK for me to declare just one class level scope button variable and use it over and over again to add an event handler to all the buttons ? This code seems to work on my developement platform, but if I try it on a new machine (deployment machine), it fails. I initially thought it was a problem with the Office 2003 PIAs, but the add-in loads and performs ALL other functions as expected.

    S.Vidyaraman

  2. Mark Bower says:

    You should add a variable for each button, for basically the same reason. Each time you change your variable to reference a different CommandBarButton object you are causing the original reference to go out of scope.

  3. S.Vidyaraman says:

    Thanks again. Are we going to see any posts on this blog relating to IRM in Office 2003 ? or is there a better place to look for IRM related discussions ?

    S.Vidyaraman

  4. Mark Bower Tuesday posted a simple tip for a very very very common question:This is perhaps the most frequently asked question I hear when writing managed COM Addins or Smart Docs.  You’ve wired up an event handler to catch button clicks of custom menu items.  They work for the first few clicks but then mysteriously stop working.  What’s going on?The answer is almost always that the CommandBarButton objects have been scoped at the method level.  What typically happens is that some indeterminate time after these objects go out of scope, the garbage collector runs and cleans up these objects along with the event hooks you have wired into.The solution is to make sure you scope these objects as class-level variables so that they stick around until the class gets destroyed. …

  5. VS Addin: Fast Add Reference Dialog – No more Coffee Break, Steve!

  6. Tuna Toksoz says:

    I have spent a couple of days on implementing a fast add reference dialog box for Visual Studio. The

  7. VS Addin: Fast Add Reference Dialog – No more Coffee Break, Steve!

Skip to main content