Outlook 2010 addin C# public methods
I need to develop an Outlook 2010 add-in and I am new to Visual Studio and C#, as I mostly use PHP and JavaScript. I am using Visual Studio 2010 and I've created a project using built-in Outlook 2010 add-in template. Consider the folowing 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 do I call public methods from ThisAddIn class in Ribbon1 class or anywhere else? I know that I need an object reference, but how can I find out the name of an instance? I can't see an instance of ThisAddIn being created anywhere in existing files. Or do I misunderstand the concept and it should be done in other ways? I would appreciate any advice or links to information on creating Office add-ins too.
In VSTO projects, an auto-generated sealed class called Globals
is available from anywhere within your project. Globals
contains a number of public or internal static properties, one of which is ThisAddIn
(of type ThisAddIn
, appropriately enough). Instead of the above, your code would then look something like below.
In Ribbon1.cs:
public void DoSomethingOnRibbon(Office.IRibbonControl control)
{
string count = Globals.ThisAddIn.displayCount();
...
}
Hope that helps.
I use a static member variable (with an associated static getter) that is set upon initialization of the add-in: then I can access it as Core
(choose name as appropriate) from anywhere in the code-base. Of course, I try to pass around the add-in object if it is available in context, but sometimes it's hard to do.
The class is instantiated automatically by the add-in container/loader (it is really exposed as a COM component, at least this is 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();
精彩评论