Answered Outlook's Addin question

  • Friday, March 02, 2012 12:23 PM
     
      Has Code

    Fellows

    I’m confused about Outlook’s Addin structure.

    I’ve created an Outlook’s Addin.

    The Outlook’s Addin template generates a 'ThisAddin.cs' class.

    In order to invoke a COM method inside Outlook with a macro, there is an override of
    the 'RequestComAddInAutomationService' method, instantiating the com object.

    See the code below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    namespace OutlookVerificaItensExcluidos
    {
        public partial class ThisAddIn
        {
           private ItensExcluidos myAddin;
           
           private void ThisAddIn_Startup(object sender, System.EventArgs e)
           {
           }
           private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
           {
           }
           protected override object RequestComAddInAutomationService()
           {
              if (myAddin == null)
              {
                 myAddin = new ItensExcluidos();
              }
              return myAddin;
           }
            #region VSTO generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    In this class, the Outllok’s Application object is exposed as on the 'RequestComAddInAutomationService' method, for instance.

    Why in other derived classes, as in the methods of the 'ItensExcluidos' class, instantiated in the overrided method, the same object isn’t seen anymore?

    Homero


    Homero OM

All Replies

  • Friday, March 02, 2012 1:31 PM
     
     

    Hi Homero,

    I am afraid that I don't understand your question correctly. What is the problem, how are you using the automation service? Is the ItensExcluidos correct COM class?


    Toni Petrina
    My blog: Programming ramblings
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

  • Friday, March 02, 2012 3:16 PM
     
      Has Code

    Tony

    The Addin itself works OK.

    I intend to use Outlook's Object Model in a method Invoked through VBA.

    The Addin implements this call OK.

    Inside the method invoked by VBA (and coded inside the Addin), though, I need to use Outlook's Object Model to

    perform some functions in Outlook.

    To get a reference to Outlook's Object Model, we generally have to get a reference to an application object (Outlook itself)

    before.

    If I code this a statement to do this, as bellow:

    Outlook.Application app = Application;

    The compiler warns me that I'm 'using a type like a variable'.

    If, instead, I code the very same statement in the 'RequestComAddInAutomationService' method, compiler accepts it gracefully.

    I'm confused with this behaviour.

    Is this an Outlook's Addin or isn't it?

    Why it seems that I can't get an Outlooks reference outside 'ThisAddin.cs' class?

    I hope this answer explains things better.

    Thanks for your time.

    Homero

     


    Homero OM

  • Friday, March 02, 2012 4:36 PM
     
     Answered Has Code

    You can get the addin via:

    Globals.ThisAddIn
    

    And you can access Outlook via:

    Globals.ThisAddIn.Application;
    

    Toni Petrina
    My blog: Programming ramblings
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    • Marked As Answer by HomeroOM Friday, March 02, 2012 5:47 PM
    •  
  • Friday, March 02, 2012 5:49 PM
     
     

    Toni

    Thanks.

    It worked!

    Just for curiosity, why Globals?

    Is it to avoid confusion with the Addin class created by default by the Addin Template?

    Homero


    Homero OM

  • Saturday, March 03, 2012 9:51 AM
     
     
    Probably just as an ease of access since Outlook application is singleton and access to it can be global. You cannot run Outlook addin without Outlook.

    Toni Petrina
    My blog: Programming ramblings
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"