NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.
MIT License
695 stars 143 forks source link

Outlook Events #426

Closed InteXX closed 1 month ago

InteXX commented 1 month ago

Is Outlook event handling available, e.g. the ability to respond when an email is moved to a folder?

I think it must be. I've located NetOffice.OutlookApi.Items.ItemAddEvent in the source, but it's not clear how to get to that in my consuming code.

This code fails to compile:

Dim oInboxFolder As MAPIFolder
Dim oOutlookApp As Application
Dim oNameSpace As _NameSpace

oOutlookApp = New Application
oNameSpace = oOutlookApp.GetNamespace("MAPI")
oInboxFolder = oNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox)

AddHandler oInboxFolder.Items.ItemAddEvent, AddressOf Items_ItemAdd ' <-- Error here 'ItemAddEvent' is not a member of '_Items'.

Again, I'm not finding any documentation for this.

Please advise.

InteXX commented 1 month ago

I found the answer here:

private void Initialize()
{
  outlookApp = new Outlook.Application();

  outlookNS = outlookApp.GetNamespace("MAPI");
  inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

  items = (Outlook.Items) inboxFolder.Items;
  items.ItemAddEvent +=
    new Outlook.Items_ItemAddEventHandler(items_ItemAdd);
}

We have to cast oInboxFolder.Items to OutlookApi.Items first.