I have a problem where my code runs as intended when Outlook is opened and I test stuff. But I have created a Macro that should run when Outlook starts, however it goes into runtime error "Run time error -2146950355 (80080005): Server Execution Failed"
The code fails at:
Set myNameSpace = myOlApp.GetNamespace("MAPI")
If i simply press resume, it continues and runs completely fine until next time the program is opened and the code is run automatically. I've tried calling the module sub from both the "thisOutlookSession" Applicaiton_MAPILogonComplete() trigger or the Application_Startup() - both same result.
What am i missing?
Tried adding
Set myOlApp = CreateObject("Outlook.Application")
and Set myNameSpace = myOlApp.Session
to avoid MAPI call if that helped. But without luck
Special thanks to Axel Kemper for submitting an answer which got me to the solution.
Do not take this answer for good, but purely my experience:
The following code worked as intended, but only not when called at startup by embedded sub Application_MAPILogonComplete()
Public myOlApp As New Outlook.Application
Public myNameSpace As Outlook.NameSpace
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Axel's code seemed to bypass the declaration of myOlApp, which I used the following code that works for me:
Public myNameSpace As Outlook.NameSpace
Set myNameSpace = Application.GetNamespace("MAPI")
I did not have to use myOlApp elsewhere in my code.