Click here to Skip to main content
15,881,204 members
Articles / Operating Systems / Windows
Article

Microsoft Office Version Detector

Rate me:
Please Sign up or sign in to vote.
4.55/5 (9 votes)
3 Dec 2006CPOL2 min read 88.6K   2.2K   43   12
Programmatically determine the version of Microsoft Office applications

Sample Image - OfficeVersion.png

Introduction

OfficeVersion is a simple C++ class to programmatically detect the presence, and version of the popular Microsoft Office applications.

Why would you want to do this? Perhaps you have an Excel-automation feature in your application that you want to Enable/Disable? Maybe your application uses a visual style that is similar to one of the Microsoft Office applications, and you want to change your interface to match the particular version of Office that the user has installed.

Applications and Versions

The following Microsoft Office applications are detected:

  • Word
  • Excel
  • Outlook
  • Access
  • PowerPoint
The following versions of Microsoft Office are detected:
  • Office 95
  • Office 97
  • Office 2000
  • Office XP
  • Office 2003
  • Office 2007

Multiple versions?

If there are multiple versions of an application installed, the version that is detected is the one that is "registered" (i.e. the version of the application that will open your file when you double-click the file).

Usage

  1. Add "OfficeVersion.h" to your project (only this one file is needed). Currently the code uses the MFC/ATL CString class, but this could easily be changed to another C++ string class.
    C++
    #include "OfficeVersion.h"
  2. The following example code checks for the version of Excel (Note: the bold parts of the text are just there to highlight particular parts of the code).
    C++
    OfficeVersion::eOfficeVersion excelVersion = 
    OfficeVersion::GetApplicationVersion(OfficeVersion::eOfficeApp_Excel); 
    // excelVersion will now be one of the following values: 
    // 
    // OfficeVersion::eOfficeVersion_Unknown,
                    // not found, or an error occurred 
    // OfficeVersion::eOfficeVersion_95 
    // OfficeVersion::eOfficeVersion_97 
    // OfficeVersion::eOfficeVersion_2000 
    // OfficeVersion::eOfficeVersion_XP 
    // OfficeVersion::eOfficeVersion_2003 
    // OfficeVersion::eOfficeVersion_2007 
  3. The following example code checks for the version of "Office" (note: the bold parts of the text are just there to highlight particular parts of the code).
    C++
    OfficeVersion::eOfficeVersion officeVersion = 
            OfficeVersion::GetOfficeVersion();
    //
    office Version will now be one of the following values:
    //
    // OfficeVersion::eOfficeVersion_Unknown, 
                    // not found, or an error occurred
    // OfficeVersion::eOfficeVersion_95
    // OfficeVersion::eOfficeVersion_97
    // OfficeVersion::eOfficeVersion_2000
    // OfficeVersion::eOfficeVersion_XP
    // OfficeVersion::eOfficeVersion_2003
    // OfficeVersion::eOfficeVersion_2007

Algorithm note

Since "Office" is a collection of applications, and not a single application itself, the algorithm for determining the presence and version of "Office" is to look through the set of Office applications, and use the first one found. The search is performed in order of the (approximate) popularity of the application (i.e. Word then Excel then Outlook then Access then PowerPoint).

How does it work?

The code is based on an outdated Microsoft knowledge base article Q247985 (the code in the knowledge base article does not work for Office 2003 or 2007). The code looks for a specific registry key which holds the version for each application (e.g. HKEY_CLASSES_ROOT/Excel.Application/CurVer for Excel) which contains the version encoded in a string ("Excel.Application.11" on my computer). The internal Microsoft version number (i.e. the "11" at the end of "Excel.Application.11" ) is then mapped to the external "marketing" name, that you will be more familiar with (e.g. the internal version "11" is more commonly known as "Office 2003").

History

  • 2006-12-04: Version 1.0 - Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
www.IconsReview.com[^]
Huge list of stock icon collections (both free and commercial)

The picture is from September 2006, after picking up a rental car at the airport in Denver, Colorado. I'm smiling in the picture, because I have yet to come to the realization that I just wasted 400 bucks ( because you don't really need a car in downtown Denver - you can just walk everywhere).

Comments and Discussions

 
QuestionA question about office upgrade Pin
Member 1055138126-Jan-14 19:48
Member 1055138126-Jan-14 19:48 
GeneralVB Version Pin
Daniel Leykauf24-Jul-09 11:20
Daniel Leykauf24-Jul-09 11:20 
Hi,

Below my VB source to detect the current MS Office version:
Imports Microsoft.Win32

''' <summary>
''' Purpose:       Returns current MS Office version
''' Created by:    Daniel Leykauf
''' On:            July 24, 2009
''' Note:          The code is provided 'as it is' without any garanty!
'''                Using at your own risk.
''' ------------------------------------------------------------------------             
''' </summary>
''' <remarks></remarks>
Public Class OfficeDetector
    ''' <summary>
    ''' Possible Office apps.
    ''' </summary>
    ''' <remarks></remarks>
    Enum MSOfficeApp
        Access_Application
        Excel_Application
        Outlook_Application
        PowerPoint_Application
        Word_Application
        FrontPage_Application
    End Enum

    ''' <summary>
    ''' Possible versions
    ''' </summary>
    ''' <remarks></remarks>
    Enum Version
        Version2007 = 12
        Version2003 = 11
        Version2002 = 10
        Version2000 = 9
        Version97 = 8
        Version95 = 7
    End Enum

    ''' <summary>
    ''' Lists all available MS Office apps and version in console window
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub ListAllOfficeVersions()
        For Each s As String In [Enum].GetNames(GetType(MSOfficeApp))
            Console.WriteLine(s & vbTab & GetVersionsString([Enum].Parse(GetType(MSOfficeApp), s)))
        Next
    End Sub

    ''' <summary>
    ''' Returns version number as integer
    ''' </summary>
    ''' <param name="app"></param>
    ''' <returns></returns>
    ''' <remarks>Value is 0 if no version could be detected</remarks>
    Public Shared Function GetVersionsID(ByVal app As MSOfficeApp) As Integer
        Dim strProgID As String = [Enum].GetName(GetType(MSOfficeApp), app)
        strProgID = Replace(strProgID, "_", ".")
        Dim regKey As RegistryKey
        regKey = Registry.LocalMachine.OpenSubKey("Software\Classes\" & strProgID & "\CurVer", False)
        If IsNothing(regKey) Then Return 0
        Dim strV As String = regKey.GetValue("", Nothing, RegistryValueOptions.None)
        regKey.Close()

        strV = Replace(Replace(strV, strProgID, ""), ".", "")
        Return CInt(strV)
    End Function

    ''' <summary>
    ''' Returns the version string
    ''' </summary>
    ''' <param name="app"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetVersionsString(ByVal app As MSOfficeApp) As String
        Dim strProgID As String = [Enum].GetName(GetType(MSOfficeApp), app)
        strProgID = Replace(strProgID, "_", ".")
        Dim regKey As RegistryKey
        regKey = Registry.LocalMachine.OpenSubKey("Software\Classes\" & strProgID & "\CurVer", False)
        If IsNothing(regKey) Then Return "No version detected."
        Dim strV As String = regKey.GetValue("", Nothing, RegistryValueOptions.None)
        regKey.Close()

        strV = Replace(Replace(strV, strProgID, ""), ".", "")
        Return [Enum].GetName(GetType(Version), CInt(strV))
    End Function
End Class

Cheers,

Daniel
GeneralFileVersionInfo Pin
luisnike1925-Mar-08 7:04
luisnike1925-Mar-08 7:04 
GeneralRe: FileVersionInfo Pin
Warren Stevens26-Mar-08 15:00
Warren Stevens26-Mar-08 15:00 
Generaldetect the office version with vb.net Pin
rajneesh00729-Nov-07 21:53
rajneesh00729-Nov-07 21:53 
Questionversion of MS Office through .Net web Application 2003 Pin
azabaig2-Nov-07 23:25
azabaig2-Nov-07 23:25 
General.net Pin
Gary Howlett10-Feb-07 12:01
Gary Howlett10-Feb-07 12:01 
GeneralRe: .net Pin
Warren Stevens20-Feb-07 12:05
Warren Stevens20-Feb-07 12:05 
GeneralUser Access Pin
Oleg A.Lukin4-Dec-06 22:02
Oleg A.Lukin4-Dec-06 22:02 
GeneralRe: User Access Pin
Warren Stevens5-Dec-06 3:38
Warren Stevens5-Dec-06 3:38 
GeneralSlight fix Pin
Birgir K4-Dec-06 13:27
Birgir K4-Dec-06 13:27 
GeneralRe: Slight fix Pin
Warren Stevens4-Dec-06 14:52
Warren Stevens4-Dec-06 14:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.