Referencing "OutlookApp" object on another form

Add-in Express™ Support Service
That's what is more important than anything else

Referencing "OutlookApp" object on another form
 
Fumi




Posts: 12
Joined: 2020-12-12
In "AddinModule.vb", to reference Outlook object, I can simply call "OutlookApp" below (see #1 below).


Private Sub AdxRibbonButton1_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean) Handles AdxRibbonButton1.OnClick

   Dim olApp As Object = OutlookApp '#1
   Dim olNameSpace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
....
....
....

End Sub



But, I want to reference the same "OutlookApp" on another form (See #2), but it fails because it does not exist. Instead, I could create Outlook object (See #3), which seems to be working fine.

Private Sub AdxRibbonButton1_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean) Handles AdxRibbonButton1.OnClick

   Dim fmForm As New fmMain
   fmForm.Show()


End Sub



Private Sub fmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   
   'Dim olApp As Object = OutlookApp <-- OutlookApp not available in "fmMain" object #2
   Dim olApp As Object = CreateObject("Outlook.Application") '#3
   Dim olNameSpace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
   ....
   ....
   ....
End Sub


If I remember correctly, I read an article on your website that I should always reference to "OutlookApp" object, instead of creating a new Outlook object. Is that the correct understanding?

If so, what is the best way to reference "OutlookApp" on another form? Should I try to pass the object to the another form?

Thanks.
Posted 12 Dec, 2020 15:41:35 Top
Andrei Smolin


Add-in Express team


Posts: 18338
Joined: 2006-05-11
Hello Fumi,

Fumi writes:
Is that the correct understanding?


Yes.

Use this construct:

{add-in project name such as MyAddin1}.AddinModule.CurrentInstance.{property or method declared on the add-in module such as ExcelApp}

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 14 Dec, 2020 03:45:31 Top
Fumi




Posts: 12
Joined: 2020-12-12
Andrei, great, thanks!
Posted 15 Dec, 2020 10:59:31 Top
Andrei Smolin


Add-in Express team


Posts: 18338
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Dec, 2020 03:46:00 Top