Netoffice - Outlook Add-in - Access Folder Events

424 views Asked by At

I have a VBA project for Microsoft Office Outlook, which I'd like to rewrite as an Outlook Add-in with the help of NetOffice.

Here's a piece of VBA code which I'd like to transfer:

Dim objNS As Outlook.NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set m_colCalendarItems = objNS.GetDefaultFolder(olFolderCalendar).Items

Application represents the running Outlook application.

My respective code in NetOffice looks like this:

Outlook.Application objApp = Outlook.Application.GetActiveInstance();
Outlook._NameSpace objNS = (Outlook._NameSpace)objApp.GetNamespace("MAPI");
m_colCalendarItems = (Outlook.Items)objNS.GetDefaultFolder(OlDefaultFolders.olFolderCalendar).Items;

Quite a lot of casts, surely this can be handled better. But the main problem is that I don't get a reference to the running application in the first line (objApp is null). Although this code is in the Addin_OnStartupComplete routine.

Any tips on how to set this up better?

1

There are 1 answers

0
Christian Treffler On

I found the solution. The code snippet I postet is running in a class method. It is called from the Addin_OnStartupComplete in the Addin class (Auto-generated by the NetOffice Developer Toolbox). I can get a reference to the running application: It's the Application property of the Addin class. I can provide this to the called method:

public class Addin : Outlook.Tools.COMAddin // this was auto-generated by the NetOffice Developer Toolbox
{
    FolderEvents m_folderevents = new FolderEvents(); // 'FolderEvents' is my class

    // additional auto-generated code removed

    private void Addin_OnStartupComplete(ref Array custom)
    {
        m_folderevents.InitFolders(this.Application);
    }
}