none
Event Handlers redux RRS feed

  • Question

  • Ok.  So I have the following event handlers in the Startup section of the class, and get the desired behavior, BUT the event handler only fires once and then goes out of scope.  The solution, I am told, is to declare them in the class, so they don't get garbage collected. 

    I'm not exactly sure how to do that, so any guidance would be appreciated.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

               Outlook.MAPIFolder InboxFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               InboxFolder.Folders.FolderAdd += new Outlook.FoldersEvents_FolderAddEventHandler(InboxFolder_FolderAdd);

    TIA

    Wednesday, August 29, 2012 3:16 PM

Answers

  • Outlook.MAPIFolder InboxFolder = null;

    Outlook.Folders fols = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

               InboxFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

               fols = InboxFolder.Folders;
               fols.FolderAdd += new Outlook.FoldersEvents_FolderAddEventHandler(InboxFolder_FolderAdd);

     
    Just make sure to release those class level objects when they are no longer needed, or when Outlook is closing.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Tom1212" <=?utf-8?B?VG9tMTIxMg==?=> wrote in message news:ac4afd29-d38d-417c-a526-298392a919ba...

    Ok.  So I have the following event handlers in the Startup section of the class, and get the desired behavior, BUT the event handler only fires once and then goes out of scope.  The solution, I am told, is to declare them in the class, so they don't get garbage collected. 

    I'm not exactly sure how to do that, so any guidance would be appreciated.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

               Outlook.MAPIFolder InboxFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               InboxFolder.Folders.FolderAdd += new Outlook.FoldersEvents_FolderAddEventHandler(InboxFolder_FolderAdd);

    TIA


    Ken Slovak MVP - Outlook
    • Marked as answer by Tom1212 Wednesday, August 29, 2012 4:18 PM
    Wednesday, August 29, 2012 3:48 PM
    Moderator

All replies

  • Outlook.MAPIFolder InboxFolder = null;

    Outlook.Folders fols = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

               InboxFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

               fols = InboxFolder.Folders;
               fols.FolderAdd += new Outlook.FoldersEvents_FolderAddEventHandler(InboxFolder_FolderAdd);

     
    Just make sure to release those class level objects when they are no longer needed, or when Outlook is closing.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Tom1212" <=?utf-8?B?VG9tMTIxMg==?=> wrote in message news:ac4afd29-d38d-417c-a526-298392a919ba...

    Ok.  So I have the following event handlers in the Startup section of the class, and get the desired behavior, BUT the event handler only fires once and then goes out of scope.  The solution, I am told, is to declare them in the class, so they don't get garbage collected. 

    I'm not exactly sure how to do that, so any guidance would be appreciated.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

               Outlook.MAPIFolder InboxFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               InboxFolder.Folders.FolderAdd += new Outlook.FoldersEvents_FolderAddEventHandler(InboxFolder_FolderAdd);

    TIA


    Ken Slovak MVP - Outlook
    • Marked as answer by Tom1212 Wednesday, August 29, 2012 4:18 PM
    Wednesday, August 29, 2012 3:48 PM
    Moderator
  • Much thanks!

    Wednesday, August 29, 2012 4:18 PM