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.