NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OutlookApi - Can't find events (like Outlook.Items.ItemAdd)

dp250195 opened this issue · comments

I can't find a way to attach an event handler to Outlook.Items.ItemAdd. Visual Studio IntelliSense doesn't show any event handlers available for Outlook.Items. I've gone through all available examples, but I can't find any that illustrate use of event handlers. I tried adding using NetOffice.OutlookApi.Events, but this doesn't seem to add anything. Searching the entire Internet did not provide any useful answers.

Does this solution help? https://stackoverflow.com/questions/41387975/netoffice-outlook-add-in-access-folder-events

Unfortunately, this does not help.

I can see the event being declared here: https://github.com/NetOfficeFw/NetOffice/blob/main/Source/Outlook/Events/ItemsEvents.cs#L22 but I can't find a way to reference it in my code. I'm not making an AddIn though, I'm making a Windows Service app, I added the NuGet packages myself and I did not use this Toolbox being mentioned.

I found the solution. I had to typecast everything to appropriate class (like Outlook._Items to Outlook.Items), and now I can see the ItemAddEvent.

UPDATE: Adding code for reference

using Outlook = NetOffice.OutlookApi;

public class MailService
{
    // Local fields
    private Outlook.Application oApp;
    private Outlook.Explorer oExplorer;
    private Outlook.NameSpace oNS;
    private Outlook.MAPIFolder oInbox;
    private Outlook.Items oInboxItems;

    // ctor
    public MailService()
    {
        oApp = new Outlook.Application();
        oExplorer = (Outlook.Explorer)oApp.ActiveExplorer();
        oNS = (Outlook.NameSpace)oApp.GetNamespace("MAPI");
        oInbox = oNS.Folders["MyEMail@company.com"].Folders["Inbox"];
        oInboxItems = (Outlook.Items)oInbox.Items;
        oInboxItems.ItemAddEvent += OInboxItems_ItemAddEvent;
    }

    private void OInboxItems_ItemAddEvent(NetOffice.ICOMObject item)
    {
        // Code to handle ItemAdd event
    }
}