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?
I found the solution. The code snippet I postet is running in a class method. It is called from the
Addin_OnStartupComplete
in theAddin
class (Auto-generated by the NetOffice Developer Toolbox). I can get a reference to the running application: It's theApplication
property of theAddin
class. I can provide this to the called method: