Determining Installed Excel Versions
This page describes how to determine which versions of Excel are installed
on the local machine.
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).
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
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
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.
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
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
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
|
This page last updated: 3-Mar-2012. |