Page Banner

Microsoft Office – Theme Change Event via VSTO Add-In

Microsoft Office doesn’t have a theme change event in their VSTO API, so in order to detect the theme change, we need to monitor the registry key “UI Theme”.
The key can be found in the following registry path:

For Office 2013: HKEY_CURRENT_USERSOFTWAREMicrosoftOffice15.0Common
For Office 2016: HKEY_CURRENT_USERSOFTWAREMicrosoftOffice16.0Common

The value is a REG_DWORD and its range can be from 0 to 3 depending on the version of outlook installed 0 being the default theme.
If user haven’t changed the theme from start, the Key value pair might not be present.
Themes available for Office 2013
•    White = 0
•    Light Gray = 1
•    Dark Gray = 2
Themes available for Office 2016
•    Colorful = 0
•    Dark Gray = 1
•    Black = 2
•    White = 3

To monitor any change in the value, we will use “ManagementEventWatcher” class[/fusion_text]

public void StartThemeWatcher()
{
var currentUser = WindowsIdentity.GetCurrent();
WqlEventQuery query = new WqlEventQuery(string.Format(
"SELECT * FROM RegistryValueChangeEvent WHERE " +
"Hive = 'HKEY_USERS'" +
@"AND KeyPath = '{0}SOFTWAREMicrosoftOffice15.0Common' AND ValueName='UI Theme'", currentUser.User.Value));
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
watcher.EventArrived += Watcher_EventArrived;
watcher.Start();
}
private void Watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWAREMicrosoftOffice15.0Common"))
{
if (key != null)
{
object objectValue = key.GetValue("UI Theme");
if (objectValue != null)
{
int value = (int)objectValue;
MessageBox.Show(value.ToString());
}
}
}
}
catch (Exception ex)
{
}
}

[fusion_text]In case you need to detect the current outlook version in VSTO, this might be helpful
Globals.AddinName.Application.Version

The solution for theme change detection is a bit tricky, but it will serve the purpose.