Finding a Microsoft Office version with .NET

There are many ways for a developer to find the Microsoft Office version in .NET, but today we will show you one of the more complicated ways – just as an opportunity to fine-tune your C# coding skills. We will be working with the computer’s Registry system. If you are not familiar with the Registry, what its function is, or how to navigate it, please refer to the Microsoft documentation regarding Registry here. We will also do a bit of 64 bit and 32 bit checking in between. We will not go into too much detail on the bit-ness, because the chances are slim that we want to determine an Office version which is lower than 2013.

Read: Access Office 365 REST APIs Using .NET Libraries

How to Find the Office Version in C#

With all of the above being said, let’s get started:

  • Open Visual Studio and create a new C# Windows application. Name it FindOffice_Ex
  • Add a button to the form. This button we will use to get the version of Office on the user’s machine.
  • Add a new class named Office to the solution.
  • Edit it to look like the following:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace FindOffice_Ex
{
    public class Office
    {

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]

        static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out int phkResult);
        [DllImport("Advapi32.dll")]
        static extern uint RegCloseKey(int hKey);
        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
        static extern int RegQueryValueEx(int hKey, string lpValueName, int lpReserved, ref uint lpType,
            System.Text.StringBuilder lpData, ref uint lpcbData);
        private static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
        private static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);

        private Dictionary<string, string> LatestVersions = new Dictionary<string, string>();

        public Office()
        {
            LatestVersions.Add("12.0", "Office2007");
            LatestVersions.Add("14.0", "Office2010");
            LatestVersions.Add("15.0", "Office2013");
            LatestVersions.Add("16.0", "Office2017 and higher ");
        }

        private string GetVersionNumberFromRegistry()
        {
            string regVersion;
         
                regVersion = GetVersionNumberFromRegistry("SOFTWARE\\Microsoft\\Office\\");
                if (regVersion == null)
                    regVersion = GetVersionNumberFromRegistry("SOFTWARE\\Wow6432Node\\Microsoft\\Office\\");
          
            return regVersion;
        }
        private string GetVersionNumberFromRegistry(string key)
        {
            string version = null;
            foreach (string VerNo in LatestVersions.Keys)
            {
                string offPath = Reg64(HKEY_LOCAL_MACHINE, key + VerNo + "\\Excel\\InstallRoot", "Path");
                if (offPath != null)
                {
                    version = VerNo;
                    break;
                }
            }
            return version;
        }
        public string GetVersion()
        {
            string versionFromReg = GetVersionNumberFromRegistry();
            string versionInstalled = LatestVersions[versionFromReg];

            bool? Office64BitFromReg = Off64Bit("SOFTWARE\\Microsoft\\Office\\", versionFromReg);
                
            if (Office64BitFromReg == null)
                    Office64BitFromReg = Off64Bit("SOFTWARE\\Wow6432Node\\Microsoft\\Office\\", versionFromReg);
                if (Office64BitFromReg.HasValue && Office64BitFromReg.Value)
                    versionInstalled += " (64 bit)";
                else if (Office64BitFromReg.HasValue && !Office64BitFromReg.Value)
                    versionInstalled += " (32 bit)";
                else
                {
                    versionInstalled += " (Unknown bit)";
                }
 
            return versionInstalled;
        }

        private bool? Off64Bit(string key, string version)
        {
            bool? Office64BitFromReg = null;
            string Bitness = Reg64(HKEY_LOCAL_MACHINE, key + version + "\\Outlook", "Bitness");
            if (Bitness == "x86")
                Office64BitFromReg = false;
            else if ((Bitness == "x64"))
                Office64BitFromReg = true;
            return Office64BitFromReg;
        }

        private string Reg64(UIntPtr parent, string key, string prop)
        {
            int ikey = 0;
            int bit36_64 = 0x0100;
            int query = 0x0001;
            try
            {
                uint res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, query | bit36_64, out ikey);
                if (0 != res) return null;
                uint type = 0;
                uint data = 1024;
                StringBuilder buffer = new StringBuilder(1024);
                RegQueryValueEx(ikey, prop, 0, ref type, buffer, ref data);
                string ver = buffer.ToString();
                return ver;
            }
            finally
            {
                if (0 != ikey) RegCloseKey(ikey);
            }
        }
    }
}

C# Code to Find the Office Version Explained

OK, so what does the C# code in our previous example do, exactly?

First, we added the required namespaces needed for this project, especially System.Runtime.InteropServices, since we will be making use of System APIs. With System APIs we can get access to the Operating System’s inner code.

Then, we declare the APIs so that we can work with them directly in our application. For more information about the System APIs, refer to the official Microsoft documentation here.

In the class’ constructor, we populate the LatestVersions dictionary with some versions of Office. Note that these versions range from Office 2007 to Office 365. Obviously, there are earlier versions, but we did not see the need to cater to them.

The following methods retrieve the Office version from the system’s Registry and determines if the version is 64-bit. Yes, a programmer could also test for a 64-bit operating system, but they are quite common these days, so we did not add that functionality.

Go back to your form and edit the button’s click event to the following, which allows us to get the value displayed inside a message box:

        private void button1_Click(object sender, EventArgs e)
        {
            string RunningOfficeVersion = new Office().GetVersion();
            MessageBox.Show("Running Office Number: " + RunningOfficeVersion);
        }

Now, when you run this program, a pop-up will appear showing you which version of Microsoft Office you are currently running.

Read more C# programming tutorials and software development guides.

Looking to upgrade to Microsoft Office 365?

 

Disclaimer: We may be compensated by vendors who appear on this page through methods such as affiliate links or sponsored partnerships. This may influence how and where their products appear on our site, but vendors cannot pay to influence the content of our reviews. For more info, visit our Terms of Use page.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read