Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the first try at a custom ribbon change to outlook built from a Outlook 2013 Add-In. I can get the buttons to show, but what I need to do is only enable the button at the TabMail level if an email is actually selected. If there was no email in the folder, then it would be disabled.

Here is my XML:
<pre lang="xml">
XML
<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon>
    <tabs>
      <tab idMso="TabReadMessage">
        <group id="group1" label="Regarding">
          <gallery id="glrRegarding" label="Set Regarding"  size="large" columns="1" imageMso="CopyToPersonalTaskList">
            <button id="idHiringMgr" label="Hiring Manager" onAction="button1_Click" imageMso="NewTaskRequest" />
            <button id="idCandidate" label="Candidate" onAction="button1_Click" imageMso="NewTaskRequest" />
          </gallery>
        </group>
      </tab>
      <tab idMso="TabNewMailMessage">
        <group id="group2" label="Regarding">
          <gallery id="glrRegarding2" label="Set Regarding"  size="large" columns="1" imageMso="CopyToPersonalTaskList">
            <button id="idHiringMgr2" label="Hiring Manager" onAction="button1_Click" imageMso="NewTaskRequest" />
            <button id="idCandidate2" label="Candidate" onAction="button1_Click" imageMso="NewTaskRequest" />
          </gallery>
        </group>
      </tab>
      <tab idMso="TabMail">
        <group id="group3" label="Regarding">
          <gallery id="glrRegarding3" label="Set Regarding"  size="large" columns="1" imageMso="CopyToPersonalTaskList" getEnabled = "GetEnabled">
            <button id="idHiringMgr3" label="Hiring Manager" onAction="button1_Click" imageMso="NewTaskRequest" />
            <button id="idCandidate3" label="Candidate" onAction="button1_Click" imageMso="NewTaskRequest" />
          </gallery>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>



My Ribbon CS File:

C#
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new Ribbon1();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace OutlookDMAPlugIn
{
    [ComVisible(true)]
    public class Ribbon1 : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        private RibbonUtilities ribbonUtil = new RibbonUtilities();

        private Outlook.Application olApplication;
        bool bEnable;

        Outlook.Explorer _Explorer;

        public Ribbon1(Outlook.Application outlookApplication)
        {
            olApplication = outlookApplication;
           
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("Ribbon_Update_At_Runtime.Ribbon1.xml");
        }

        #endregion

        #region Events
        void _Explorer_SelectionChange()
        {
            bEnable = false;
            foreach (Outlook.MailItem mailItem in _Explorer.Selection)
            {
                ribbon.InvalidateControl("glrRegarding3");
            }
        }
        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
            bEnable = true;
            ribbon.InvalidateControl("glrRegarding3");
            Globals.ThisAddIn.Application = new Microsoft.Office.Interop.Outlook.Application();
            _Explorer = Globals.ThisAddIn.Application.ActiveExplorer();
            _Explorer.SelectionChange += _Explorer_SelectionChange;
        }
        
        private bool GetEnabled(Office.IRibbonControl control)
        {
            return bEnable;
        }

        /// <summary>
        /// Form to search with
        /// </summary>
        /// <param name="control"></param>
        public void button1_Click(Office.IRibbonControl control)
        {
            ribbonUtil.ActionToDo(control.Id);
        }

        #endregion

        #region Helpers

        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
    }
}


And ThisAddIn CS File

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 Ribbon_Update_At_Runtime.User_Controls;
using System.Windows.Forms;
using System.Diagnostics;

namespace OutlookDMAPlugIn
{
public partial class ThisAddIn
{
#region Instance Variables
Outlook.Application m_Application;
// List of tracked explorer windows

// Ribbon UI reference
internal static Office.IRibbonUI m_Ribbon;
#endregion

#region VSTO Startup and Shutdown methods
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Initialize variables
m_Application = this.Application;

// Add the ActiveExplorer to m_Windows
Outlook.Explorer expl = m_Application.ActiveExplorer()
as Outlook.Explorer;
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
m_Ribbon = null;
m_Application = null;
}

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon1(m_Application);
}

#endregion

#region Methods


#endregion
#region VSTO generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}

Ive searched online and everyone says to use ribbon.InvalidateControl, but I cannot get that to work. Any help would be appreciated.

Thank you,
Ryan
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900