L
lhsu
GOAL: I am writing an add-in that basically monitors all events fired by all
outlook objects (inspectors, explorers, application, items, item,
folder...etc), as well as button clicks for the command bar buttons.
PROBLEM: Events stop firing. My event handler does nothing intensive, it
merely writes the event that occured to a logfile using a streamwriter.
Specifically, after I added support to handle button clicks, explorer and
inspector events, I can only catch a total of 4 button click or folder_switch
events, and thereafter, the same actions no longer generate events! My
application level events (such as NewMailEx and Item_Send) continue to fire.
QUESTION: Does outlook flush some kind of event handler delegate que? Do
you need to re-assign your delegates to the event after some time? I could
not find any information in the vbaol file. My code is C#. Below is a
sample of the code.
Any help would be greatly appreciated. Thank you.
public void OnStartupComplete(ref System.Array custom)
{
//add Event Handlers
outlookApp.NewMailEx += new
ApplicationEvents_11_NewMailExEventHandler(OutlookEventHandlers.outlookApp_NewMailEx);
outlookApp.ItemSend += new
ApplicationEvents_11_ItemSendEventHandler(OutlookEventHandlers.outlookApp_ItemSend);
//folders events
//items events
RegisterFoldersAndItemsEventHandlers(outlookApp.Session.Folders);
//explorer events
//outlookApp.Explorers.NewExplorer += new
ExplorersEvents_NewExplorerEventHandler(OutlookEventHandlers.explorers_NewExplorer);
foreach (Microsoft.Office.Interop.Outlook.Explorer exp in
outlookApp.Explorers)
{
RegisterExplorerEventHandlers(exp);
}
try
{
//ACTIONS MENU
Microsoft.Office.Core.CommandBarButton newButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[1];
newButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton replyButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[7];
//Microsoft.Office.Core.CommandBarButton replyButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.Explorers["Microsoft
Office Outlook"].CommandBars["Menu Bar"].Controls["Actions"]).Controls[7];
replyButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton replyAllButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[8];
replyAllButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton forwardButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[9];
forwardButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
//EDIT MENU
Microsoft.Office.Core.CommandBarButton deleteButton
=(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Edit"]).Controls[7];
deleteButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton undeleteButton
=(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Edit"]).Controls[8];
undeleteButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
....
}
catch (System.Exception e)
{
OutlookLogger.LogComment(fn, e.Message + " " + e.InnerException.Message);
}
}
public static void RegisterExplorerEventHandlers(Explorer exp)
{
//exp.SelectionChange += new
ExplorerEvents_10_SelectionChangeEventHandler(OutlookEventHandlers.explorer_SelectionChange);
OutlookLogger.LogComment(fn, "Explorer events");
exp.FolderSwitch += new
ExplorerEvents_10_FolderSwitchEventHandler(OutlookEventHandlers.explorer_FolderSwitch);
//exp.BeforeFolderSwitch += new
ExplorerEvents_10_BeforeFolderSwitchEventHandler(OutlookEventHandlers.explorer_BeforeFolderSwitch);
//exp.BeforeMaximize += new
ExplorerEvents_10_BeforeMaximizeEventHandler(OutlookEventHandlers.explorer_BeforeMaximize); //doesn't fire if it is in background
//exp.BeforeMinimize += new
ExplorerEvents_10_BeforeMinimizeEventHandler(OutlookEventHandlers.explorer_BeforeMinimize);
//exp.Deactivate += new
ExplorerEvents_10_DeactivateEventHandler(OutlookEventHandlers.explorer_Deactivate);
// ExplorerClass expClass = exp as ExplorerClass;
// if (expClass !=null )
// {
// expClass.ExplorerEvents_10_Event_Activate += new
ExplorerEvents_10_ActivateEventHandler(OutlookEventHandlers.explorer_Activate);
// expClass.ExplorerEvents_10_Event_Close += new
ExplorerEvents_10_CloseEventHandler(OutlookEventHandlers.explorer_Close);
// }
}
outlook objects (inspectors, explorers, application, items, item,
folder...etc), as well as button clicks for the command bar buttons.
PROBLEM: Events stop firing. My event handler does nothing intensive, it
merely writes the event that occured to a logfile using a streamwriter.
Specifically, after I added support to handle button clicks, explorer and
inspector events, I can only catch a total of 4 button click or folder_switch
events, and thereafter, the same actions no longer generate events! My
application level events (such as NewMailEx and Item_Send) continue to fire.
QUESTION: Does outlook flush some kind of event handler delegate que? Do
you need to re-assign your delegates to the event after some time? I could
not find any information in the vbaol file. My code is C#. Below is a
sample of the code.
Any help would be greatly appreciated. Thank you.
public void OnStartupComplete(ref System.Array custom)
{
//add Event Handlers
outlookApp.NewMailEx += new
ApplicationEvents_11_NewMailExEventHandler(OutlookEventHandlers.outlookApp_NewMailEx);
outlookApp.ItemSend += new
ApplicationEvents_11_ItemSendEventHandler(OutlookEventHandlers.outlookApp_ItemSend);
//folders events
//items events
RegisterFoldersAndItemsEventHandlers(outlookApp.Session.Folders);
//explorer events
//outlookApp.Explorers.NewExplorer += new
ExplorersEvents_NewExplorerEventHandler(OutlookEventHandlers.explorers_NewExplorer);
foreach (Microsoft.Office.Interop.Outlook.Explorer exp in
outlookApp.Explorers)
{
RegisterExplorerEventHandlers(exp);
}
try
{
//ACTIONS MENU
Microsoft.Office.Core.CommandBarButton newButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[1];
newButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton replyButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[7];
//Microsoft.Office.Core.CommandBarButton replyButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.Explorers["Microsoft
Office Outlook"].CommandBars["Menu Bar"].Controls["Actions"]).Controls[7];
replyButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton replyAllButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[8];
replyAllButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton forwardButton =
(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Actions"]).Controls[9];
forwardButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
//EDIT MENU
Microsoft.Office.Core.CommandBarButton deleteButton
=(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Edit"]).Controls[7];
deleteButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
Microsoft.Office.Core.CommandBarButton undeleteButton
=(Microsoft.Office.Core.CommandBarButton)((Microsoft.Office.Core.CommandBarPopup)outlookApp.ActiveExplorer().CommandBars["Menu
Bar"].Controls["Edit"]).Controls[8];
undeleteButton.Click += new
_CommandBarButtonEvents_ClickEventHandler(OutlookEventHandlers.button_Click);
....
}
catch (System.Exception e)
{
OutlookLogger.LogComment(fn, e.Message + " " + e.InnerException.Message);
}
}
public static void RegisterExplorerEventHandlers(Explorer exp)
{
//exp.SelectionChange += new
ExplorerEvents_10_SelectionChangeEventHandler(OutlookEventHandlers.explorer_SelectionChange);
OutlookLogger.LogComment(fn, "Explorer events");
exp.FolderSwitch += new
ExplorerEvents_10_FolderSwitchEventHandler(OutlookEventHandlers.explorer_FolderSwitch);
//exp.BeforeFolderSwitch += new
ExplorerEvents_10_BeforeFolderSwitchEventHandler(OutlookEventHandlers.explorer_BeforeFolderSwitch);
//exp.BeforeMaximize += new
ExplorerEvents_10_BeforeMaximizeEventHandler(OutlookEventHandlers.explorer_BeforeMaximize); //doesn't fire if it is in background
//exp.BeforeMinimize += new
ExplorerEvents_10_BeforeMinimizeEventHandler(OutlookEventHandlers.explorer_BeforeMinimize);
//exp.Deactivate += new
ExplorerEvents_10_DeactivateEventHandler(OutlookEventHandlers.explorer_Deactivate);
// ExplorerClass expClass = exp as ExplorerClass;
// if (expClass !=null )
// {
// expClass.ExplorerEvents_10_Event_Activate += new
ExplorerEvents_10_ActivateEventHandler(OutlookEventHandlers.explorer_Activate);
// expClass.ExplorerEvents_10_Event_Close += new
ExplorerEvents_10_CloseEventHandler(OutlookEventHandlers.explorer_Close);
// }
}