Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 759
noname Avatar asked Jan 19 '23 15:01

noname


1 Answers

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.

like image 99
jpb Avatar answered Jan 31 '23 01:01

jpb