Outlook 2010 addin c # public methods

I need to develop an Outlook 2010 add-in, and I'm new to Visual Studio and C #, since I mostly use PHP and JavaScript. I am using Visual Studio 2010, and I created a project using the built-in Outlook 2010 add-in template. Consider the following code:

// file ThisAddIn.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml.Linq; using Outlook = Microsoft.Office.Interop.Outlook; using Office = Microsoft.Office.Core; namespace OutlookAddIn1 { public partial class ThisAddIn { private void ThisAddIn_Startup(object sender, System.EventArgs e) { } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } public string displayCount() { Outlook.MAPIFolder inbox = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); Outlook.Items unreadItems = inbox.Items.Restrict("[Unread]=true"); return string.Format("Unread items in Inbox = {0}", unreadItems.Count); } #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 } } // file Ribbon1.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Tools.Ribbon; namespace OutlookAddIn1 { public partial class Ribbon1 { private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { } private void button1_Click(object sender, RibbonControlEventArgs e) { // call ThisAddIn.displayCount() here } } } 

The question is, how can I call public methods from the ThisAddIn class of the Ribbon1 class, or somewhere else? I know that I need an object reference, but how do I know the instance name? I do not see an instance of ThisAddIn that is being created somewhere in existing files. Or am I misunderstanding the concept, and it has to be done in other ways? I would appreciate any advice or links to information on creating Office add-ins.

+4
source share
2 answers

I use a static member variable (with an associated static getter) that is set when the add-in is initialized: then I can access it as Core (choose a name as needed) from anywhere in the code base. Of course, I am trying to convey an add-in object if it is available in context, but sometimes it is difficult to do.

The class is created automatically by the add-in container / loader (it really appears as a COM component, at least that’s how it works in ADX :).

Happy coding.


The code might look something like this:

 // inside ThisAddIn class public static ThisAddIn Active { get; private set; } // inside ThisAddIn_Startup Active = this; // later on, after add-in initialization, say in Ribbon1.button1_Click ThisAddIn.Active.displayCount(); 
+3
source

In VSTO projects, an automatically created private class called Globals is available from anywhere in your project. Globals contains a number of public or internal static properties, one of which is ThisAddIn (such as ThisAddIn , suitable enough). Instead of the above, your code will look something like this.

In Ribbon1.cs:

 public void DoSomethingOnRibbon(Office.IRibbonControl control) { string count = Globals.ThisAddIn.displayCount(); ... } 

Hope this helps.

+6
source

All Articles