Site icon bVisual

Getting the Microsoft Office Theme Value

I am working on a Visio VSTO project where I need to synchronise a UI element with the current session Microsoft Office Theme. At first, I thought that there would be some way of getting a value from the Visio object model, but I was wrong…

The Office Theme can be selected from either the Visio Options, or the Account Options panels.

As can be seen in the following slideshow, the Office Theme selected has a drastic effect on the Visio UI:

The Visio object model does have an ApplicationSettings object that has many settings that used to provide the color values of the Visio UI elements for the current session, but my tests show that these always return the same values, regardless of the Office Theme selected.

There also used to be user interface to edit these, but now, I guess, they are no longer used….

The Registry does have a number of the Visio UI elements, but not the particular ones that I am interested, such as the Drawing Background.

So, I searched the web for more insight into how I might be able to get the Office Theme colors, but the best I could find was https://stackoverflow.com/questions/60078708/is-it-possible-to-get-the-current-office-design-via-c-sharp-in-vsto-add-in . So, basically, it is possible to get the Office Theme number from the registry, but not the RGB values (unless someone knows different!).

I adapted the code in the link from the above article to read the Registry:

private static int OfficeThemeValue(string officeVersion)
{
	int uiThemeValue = 0;
	string OfficeCommonKey = $@"Software\Microsoft\Office\{officeVersion}\Common";
	RegistryKey regKey = Registry.CurrentUser.OpenSubKey(OfficeCommonKey, false);
	var subValues = regKey.GetValueNames().ToList();
	if (subValues.Contains("UI Theme"))
	{
		uiThemeValue = Convert.ToInt32(regKey.GetValue("UI Theme"));
	}

	return uiThemeValue;
}

All I need now is the Visio version number, which is available from the Application.Version property, and pas that through to the above method.

Surprisingly, the return values are:

I think that 1 and 2 where Blue and Silver in earlier versions of Office.

So, now I just had to sample the RGB values of the Drawing Background for each one:

Now, I can add these values to my code to make the changes to the UI that I want….

Exit mobile version