ThreeWave Determining Installed Excel Versions

This page describes how to determine which versions of Excel are installed on the local machine.
ShortFadeBar

Introduction

There may be time that you need to know which verisons are installed on the local machine. The simplest way to do this is to use the Dir funciton to test for the file's existence. However, the user can install Excel in to any folder on the system, so it might exist in a non-standard location. By default, Excel's file name is C:\Program Files (x86)\Microsoft Office\Version\Excel.exe, where Version is the verison number of Office (8 = Office97, ... 14 = Office12). But if you have installed the 64 bit version of Excel, it will be in Program Files not Program Files(x86). Moreover, it could be in any folder on the machine, even outside the Program Files. The way to get the versions is to look around the registry. Program Files(x86).

download You can download the file with all the example code on this page.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' modInstalledVersions
' This determines whether a particular version of Excel is
' installd on the local machine. Verison  8 will always
' be shown as installed even if it it not. This is due to
' the way Excel uses the registry.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Declare Function RegCloseKey Lib "advapi32.dll" ( _
    ByVal HKey As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" ( _
    ByVal HKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long

Const Excel97 = 8
Const Excel2000 = 9
Const Excel2002 = 10
Const Excel2003 = 11
Const Excel2007 = 12
Const Excel2010 = 14
Const Excel15 = 15

Function GetInstalledVersions(Arr As Variant) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetInstalledVersions
' This determines which versions of Excel are installed on
' the local machine. This may or may not be the version currently
' running. Excel 8 will always be shown as installed, even if it
' is not, due to the way Excel registers applications in the
' registry.
' Arr may be an array of of Booleans, dim'ed so that LBound(Arr)
' is the first version to test and UBound is the last version to
' test. Each element in the array is a boolean indicating if the
' version is installed. For example, to test for versions 8 through
' 12, you would dim the array as Dim Arr(8 To 12) A Boolean. Then,
' for example, Arr(10) would be True if Excel 2002 is installed.
' If Arr is not an array, it is a single value of the version to
' test. The function will return True or False indicating whether
' that version exisits. E.g., B = GetInstalledVersions(10) will
' test whether verion 10 exists. If Arr is an array, the function
' will always return True.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Res As Long
Dim HKey As Long
Dim N As Long

Const HKEY_LOCAL_MACHINE As Long = &H80000002
Const HKEY_CURRENT_USER As Long = &H80000001
Const HKEY_CLASSES_ROOT = &H80000000
Const HKCU = HKEY_CURRENT_USER
Const KEY_QUERY_VALUE = &H1
Const ERROR_SUCCESS = 0&
Const ERROR_KEY_NOT_FOUND = 2&
Const ERROR_ACCESS_DENIED = 5
Const S_OK = &H0

If IsArray(Arr) = False Then
    Res = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Office\" & _
            Format(CInt(Arr), "0.0"), 0&, KEY_QUERY_VALUE, HKey)
    If Res = ERROR_SUCCESS Then
        GetInstalledVersions = True
    Else
        GetInstalledVersions = False
    End If
    Exit Function
End If

For N = LBound(Arr) To UBound(Arr)
    Res = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Office\" & _
            Format(N, "0.0"), 0&, KEY_QUERY_VALUE, HKey)
    If Res = ERROR_SUCCESS Then
        Arr(N) = True
    Else
        Arr(N) = False
    End If
Next N
RegCloseKey HKey:=HKey
GetInstalledVersions = True

End Function

SectionBreak

Using The Registry

We can use the system registry to find what versions of Excel are installed. When you install a version of Office, the installer creates the key HKEY_LOCAL_MACHINE\Software\Microsoft\OfficeNN where NN is the verison of Office being installed. We can search for as many version numbers as we want to test for a whole range of Office installations. Note that due to the way Excel is organized, Excel verison (97) is always shown as installed, even if it is not present on the machine.

SectionBreak

The Code

Below is a function that looks in the registry for Office installations. It take an optional argument that may be a single Long variable or an array of Boolean values. If you pass to the function a single Long, that value should be the version number you are testing for. To test for multiple versions, pass in an array whose LBound is the earliest version to test for and the UBound is the latest version. On input, the values of the array are irrelevant. On output, each element of the array indicates whether that version is installed. For example,

Dim Arr(8 To 15) As Boolean
Dim B as Boolean
B = GetInstalledVersions(Arr)

will populate the array with True and False values indicating wheter that version is installed. In the example above, Arr(10) will indicate whether Excel 10 is installed. When an array is passed to the function, the function always returns True. A more complete example is shown below:

Sub Test()
    Dim Arr() As Boolean
    Dim Version As Long
    Dim N As Long
    Dim B As Boolean
    ' test one verison
    Version = 14 ' Excel 2010
    B = GetInstalledVersions(Version)
    If B = True Then
        Debug.Print "version: " & CStr(Version) & " is installed."
    Else
        Debug.Print "version: " & CStr(Version) & " is installed."
    End If
    
    ' test multiple versions
    ReDim Arr(5 To 14)
    B = GetInstalledVersions(Arr)
    For N = LBound(Arr) To UBound(Arr)
        Debug.Print "Verison " & CStr(N) & " exists: " & Arr(N)
    Next N
End Sub
download You can download the file with all the example code on this page.
ShortFadeBar
LastUpdate This page last updated: 3-Mar-2012.

-->