简体   繁体   中英

Call function every time you enter an e-mail in Sent Items - Outlook - VSTO

I am trying to make it so that when an e-mail goes to the "Sent Items" box, it calls a function that will get the "Sent Date" of the e-mail. But it is not working. Here is the code of how it is being done to add the method to be called.

private void ThisAddIn_Startup(object sender, System.EventArgs e) {
        try
        {
            Outlook.Stores stores = Application.Session.Stores;
            foreach (Outlook.Store store in stores)
            {
                var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                var items = folder.Items;
                if (items.Count > 0)
                    MessageBox.Show($"Unread items in Inbox = {items.Count}");
                items.ItemAdd += ItemsAdd;
            }

        }
        catch (Exception exception)
        {
            Log.Error(exception.Message);
            throw;
        }
}

I am trying to make it so that when an e-mail goes to the "Sent Items" box, it calls a function that will get the "Sent Date" of the e-mail. But it is not working. Here is the code of how it is being done to add the method to be called.


UPDATE

I tried using the variable items as a global variable, however it keeps in theory adding the function in sent items but it does not call the function when entering something in sent items .

Code below.

    Outlook.Items items;

    private void ThisAddIn_Startup(object sender, System.EventArgs e) {
        try
        {
            Outlook.Stores stores = Application.Session.Stores;
            foreach (Outlook.Store store in stores)
            {
                var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                items = folder.Items;
                
                items.ItemAdd += ItemsAdd;
            }

        }
        catch (Exception exception)
        {
            Log.Error(exception.Message);
            throw;
        }
    }

First, you need to get the Sent Items folder, not Inbox like shown in your code:

 var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

Get the Sent Items instead:

 var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);

Second, you must declare the source object at the global scope if you want to get event fired. Otherwise, your source object (an instance of the Items class) will be swiped from the heap by the garbage collector (GC). See the difference in the source code:

// declare the source object at the class level
Outlook.Items items;

private void ThisAddIn_Startup(object sender, System.EventArgs e) {
        try
        {
            Outlook.Stores stores = Application.Session.Stores;
            foreach (Outlook.Store store in stores)
            {
                var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                
                // set the reference in the code 
                items = folder.Items;
                if (items.Count > 0)
                    MessageBox.Show($"Unread items in Inbox = {items.Count}");
                // subscribe to the event
                items.ItemAdd += ItemsAdd;
            }

        }
        catch (Exception exception)
        {
            Log.Error(exception.Message);
            throw;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM