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">
="1.0"="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:
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;
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
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;
}
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