Sophia
Sophia

Reputation: 63

How can I implement CallBack Methods in the Ribbon of my Outlook AddIn?

For an university project I have to implement an Outlook AddIn with a customized Ribbon. In the Ribbon the User should have the possibility to switch the different functions of the AddIn on and off. Therefore I need CallBack Methods. Unfortunately I'm not that good in C# and need help. This is my code for the Ribbon so far:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using System.Collections.Specialized;
using Microsoft.Office.Interop.Outlook;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq.Expressions;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Data;
using System.IO;
//using Ribbon_Update_At_Runtime.Northwind40DataSetTableAdapters;

namespace Add_In
{
    [ComVisible(true)]
    public class RibbonSettings : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        private StringCollection m_internalDomains;
        private StringCollection m_anhangBlacklist;
        private string m_vertraulichkeitsvermerk;
        private bool[] m_lines = new bool[22];
        private bool m_attachmentCheck;
        private bool m_confidentialCheck;
        private bool m_externalCheck;
        private bool m_recipientCheck;
        private bool m_nameCheck;
        private bool m_timedelayedCheck;
        public delegate bool CallBack();

        public RibbonSettings()
        {
        }

        public enum RibbonBoxStyle { }

        #region getter & setter
        public StringCollection get_anhangBlacklist()
        {
            return m_anhangBlacklist;
        }

        public void set_anhangBlacklist(StringCollection value)
        {
            m_anhangBlacklist = value;
        }

        public StringCollection get_internalDomains()
        {
            return m_internalDomains;
        }

        public void set_internalDomains(StringCollection value)
        {
            m_internalDomains = value;
        }

        public string get_vertraulichkeitsvermerk()
        {
            return m_vertraulichkeitsvermerk;
        }
        public void set_vertraulichkeitsvermerk(string value)
        {
            m_vertraulichkeitsvermerk = value;
        }

        public bool get_attachmentCheck()
        {
            return m_attachmentCheck;
        }
        public void set_attachmentCheck(bool value)
        {
            m_attachmentCheck = value;
        }
        public bool get_confidentialCheck()
        {
            return m_confidentialCheck;
        }
        public void set_confidentialCheck(bool value)
        {
            m_confidentialCheck = value;
        }
        public bool get_externalCheck()
        {
            return m_externalCheck;
        }
        public void set_externalCheck(bool value)
        {
            m_externalCheck = value;
        }
        public bool get_recipientCheck()
        {
            return m_recipientCheck;
        }
        public void set_recipientCheck(bool value)
        {
            m_recipientCheck = value;
        }
        public bool get_nameCheck()
        {
            return m_nameCheck;
        }
        public void set_nameCheck(bool value)
        {
            m_nameCheck = value;
        }
        public bool get_timedelayedCheck()
        {
            return m_timedelayedCheck;
        }
        public void set_timedelayedCheck(bool value)
        {
            m_timedelayedCheck = value;
        }

        #endregion

        #region IRibbonExtensibility-Member

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("Add_In.RibbonSettings.xml");
        }
        #endregion

        #region Menübandrückrufe

       
        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        CallBack myCallBack = new CallBack();

        private EnumWindows(myCallBack);

        #endregion

        public void OnInternalDomains(Office.IRibbonControl control) //onAction event (button clicked) of the Domains-Button
        {
            form_internalDomains form = new form_internalDomains(m_internalDomains);
            form.Show();
            m_internalDomains = form.get_internalDomains();
        }

        public void OnAttachmentCheck(Office.IRibbonControl control)
        {
            m_attachmentCheck = get_attachmentCheck();
        }
        public void OnConfidentialCheck(Office.IRibbonControl control)
        {
            m_confidentialCheck = get_confidentialCheck();
        }
        public void OnExternalCheck(Office.IRibbonControl control)
        {
            m_externalCheck = get_externalCheck();
        }
        public void OnRecipientCheck(Office.IRibbonControl control)
        {
            m_recipientCheck = get_recipientCheck();
        }
        public void OnNameCheck(Office.IRibbonControl control)
        {
            m_nameCheck = get_nameCheck();
        }
        public void OnTimeDelayedCheck(Office.IRibbonControl control)
        {
            m_timedelayedCheck = get_timedelayedCheck();
        }

        public void OnAnhangBlacklist(Office.IRibbonControl control) 
        {
            form_anhangBlacklist form = new form_anhangBlacklist(m_anhangBlacklist);
            form.Show();
            m_anhangBlacklist = form.get_anhangBlacklist();
        }
        public void OnVertraulichkeitsvermerk(Office.IRibbonControl control)
        {
            form_vertraulichkeitsvermerk form = new form_vertraulichkeitsvermerk(m_vertraulichkeitsvermerk);
            form.Show();
            m_vertraulichkeitsvermerk = form.get_vertraulichkeitsvermerk();
        }
        public bool EL3(Office.IRibbonControl control)
        {
            return m_lines[0];
        }
        public bool EL4(Office.IRibbonControl control)
        {
            return m_lines[1];
        }
        public bool EL5(Office.IRibbonControl control)
        {
            return m_lines[2];
        }
        public bool EL6(Office.IRibbonControl control)
        {
            return m_lines[3];
        }
        public bool EL7(Office.IRibbonControl control)
        {
            return m_lines[4];
        }
        public bool EL8(Office.IRibbonControl control)
        {
            return m_lines[5];
        }
        public bool EL9(Office.IRibbonControl control)
        {
            return m_lines[6];
        }
        public bool EL10(Office.IRibbonControl control)
        {
            return m_lines[7];
        }
        public bool EL11(Office.IRibbonControl control)
        {
            return m_lines[8];
        }
        public bool EL12(Office.IRibbonControl control)
        {
            return m_lines[9];
        }
        public bool EL13(Office.IRibbonControl control)
        {
            return m_lines[10];
        }
        public bool EL14(Office.IRibbonControl control)
        {
            return m_lines[11];
        }
        public bool EL15(Office.IRibbonControl control)
        {
            return m_lines[12];
        }
        public bool EL16(Office.IRibbonControl control)
        {
            return m_lines[13];
        }
        public bool EL17(Office.IRibbonControl control)
        {
            return m_lines[14];
        }
        public bool EL18(Office.IRibbonControl control)
        {
            return m_lines[15];
        }
        public bool EL19(Office.IRibbonControl control)
        {
            return m_lines[16];
        }
        public bool EL20(Office.IRibbonControl control)
        {
            return m_lines[17];
        }
        public bool EL21(Office.IRibbonControl control)
        {
            return m_lines[18];
        }
        public bool EL22(Office.IRibbonControl control)
        {
            return m_lines[19];
        }
        public bool EL23(Office.IRibbonControl control)
        {
            return m_lines[20];
        }
        public bool EL24(Office.IRibbonControl control)
        {
            return m_lines[21];
        }

        public string OnLinkCopyright(Office.IRibbonControl control)
        {
            return "https://git.dhbw-stuttgart.de/privacy/outlookadresses";
        }

        #region Hilfsprogramme

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}

Can anybody help me?

Thanks in advance!

Upvotes: 2

Views: 375

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

You need to define a public function with the required signature in the ribbon class. For each event the callback signature can be different. For example, the getEnabled attribute has the following signature:

C#: bool GetEnabled(IRibbonControl control)

VBA: Sub GetEnabled(control As IRibbonControl, ByRef enabled)

C++: HRESULT GetEnabled([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfEnabled)

Visual Basic: Function GetEnabled(control as IRibbonControl) As Boolean

You can find the callback signatures in the Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3) article.

Upvotes: 2

STHOH
STHOH

Reputation: 319

I interpret

User should have the possibility to switch the different functions of the AddIn on and off

as being able to enable/disable the function in the ribbon.

You can use the IRibbonUI.Invalidate or IRibbonUI.InvalidateControl methods for updating the ribbon. The response is cached for each callback the add-in implements. For example, if the add-in implements the getEnabled callback procedure for a button, the function is called once. The state remains in-place until the add-in signals that the cached values are invalid by using the Invalidate method.

So for example, if you want to enable/disable a button you could change boolean determaning whether the button should be enabled/disabled and then call ribbon.Invalidate() which updates all the callbacks.

Alternatively, you could just invalidate the specific control.

The callback function could look something like:

public bool SetEnabled(Office.IRibbonControl control)
{
    return IsEnabled;
}

And then there could be a function, that updates the bool IsEnabled like:

public void ButtonEnabled()
{
    IsEnabled = true;
    ribbon.Invalidate();
}

Upvotes: 1

Related Questions