Home Articles VB.Net Articles What versions of Excel are installed and where

What versions of Excel are installed and where

There was a question recently on Stack Overflow which I felt was a challenge to answer as I couldn't find anywhere online which appeared to give an answer on how to do it. After some time going through the registry I was able to identify how to figure out which versions of Excel were installed.

The following snippet of code is what I came up with to answer the question, the variable rtn is a Dictionary which stores the version of Excel as the key and the directory in which Excel is installed as the value.

        Dim reg As RegistryKey
        Dim subKey As RegistryKey
        Dim rtn As New Dictionary(Of String, String)

        reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Office")
        If reg IsNot Nothing Then
            For Each subKeyName As String In reg.GetSubKeyNames
                subKey = reg.OpenSubKey(subKeyName)
                If subKey IsNot Nothing Then
                    If subKey.GetSubKeyNames().Contains("Excel") Then
                        subKey = subKey.OpenSubKey("Excel\InstallRoot")
                        rtn.Add(subKeyName, subKey.GetValue("Path").ToString)
                    End If
                End If
            Next
        End If

        For Each kvp In rtn
            MessageBox.Show(String.Format("Version: {0} at '{1}Excel.exe'", kvp.Key, kvp.Value))
        Next