Click here to Skip to main content
Licence CPOL
First Posted 30 May 2008
Views 26,567
Downloads 1,209
Bookmarked 24 times

Getting Office's Version

By | 30 May 2008 | Article
Getting office's version by readint the app path key

Introduction

After building some office add-ins — I needed to create an installation for them. The problem was that I built add-ins for Word/Excel 2003, and add-ins for Word/Excel 2007, and I needed the installation to determine which of them to install. After doing some searching, I found out that the way to get this is by checking for the “Guid”s in the “Uninstall” registry keys. This was too exaggerated, since I only needed to know which Word/Excel version is installed. Another option was to check the office registry, but this only worked if I checked it on a clean computer, because a big portion of the keys remain after uninstalling office, so there’s still no certainty. Finally, I came up with (what I think is) a better solution, since it uses no “Guid”s of office-keys.

Background

Many of you probably know that in order to run Word/Excel/PowerPoint/Outlook, you can go to Start >> Run, and write “winword/excel/powerpnt/outlook”, this is possible due to registry keys, which contain their fullpaths. By getting the value of these keys, all I need is to get the information about the file’s version (12 for 2007, 11 for 2003).

Step I: Getting the Paths

First, for simplicity, i added an enum, to get the component wanted:

public enum OfficeComponent
    {
        Word,
        Excel,
        PowerPoint,
        Outlook
    }

The keys can be found in two places, the first is under the HKEY_CURRENT_USER, and the second (if we didn’t find it already) is HKEY_LOCAL_MACHINE. The sub_path is the same on both: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths. Now all we need is to get these values:

/// <summary>
/// gets the component's path from the registry. if it can't find it - retuns 
///an empty string
/// </summary>
private string GetComponentPath(OfficeComponent _component)
{
    const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths"
    string toReturn = string.Empty;
    string _key = string.Empty;

    switch (_component)
    {
        case OfficeComponent.Word:
            _key = "winword.exe";
            break;
        case OfficeComponent.Excel:
            _key = "excel.exe";
            break;
        case OfficeComponent.PowerPoint:
            _key = "powerpnt.exe";
            break;
        case OfficeComponent.Outlook:
            _key = "outlook.exe";
            break;
    }

    //looks inside CURRENT_USER:
    RegistryKey _mainKey = Registry.CurrentUser;
    try
    {
        _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
        if (_mainKey != null)
        {
            toReturn = _mainKey.GetValue(string.Empty).ToString();
        }
    }
    catch
    { }

    //if not found, looks inside LOCAL_MACHINE:
    _mainKey = Registry.LocalMachine;
    if (string.IsNullOrEmpty(toReturn))
    {
        try
        {
            _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
            if (_mainKey != null)
            {
                toReturn = _mainKey.GetValue(string.Empty).ToString();
            }
        }
        catch
        { }
    }

    //closing the handle:
    if (_mainKey != null)
        _mainKey.Close();

    return toReturn;
}

Step II: Get Files Information

Using .NET’s class FileVersionInfo, we can access many of the file’s attributes, including its version:

/// <summary>
/// Gets the major version of the path. if file not found (or any other        
/// exception occures - returns 0
/// </summary>
private int GetMajorVersion(string _path)
{
    int toReturn = 0;
    if (File.Exists(_path))
    {
        try
        {
        FileVersionInfo _fileVersion = FileVersionInfo.GetVersionInfo(_path);
        toReturn = _fileVersion.FileMajorPart;
        }
        catch
        { }
    }
    return toReturn;
}

*Note: because all I need to know is if it’s 2003 or 2007, I’m only checking the major version of the file. For those of you who want to get more specific information, use the full version of the file.

Finish

Now all you have to do is to check the FileVersion of the path (i.e: int _wordVersion = GetMajorVersion(GetComponentPath(OfficeComponent.Word));: if _wordVersion = 11 – 2003, if _wordVersion = 12 – 2007. Display it nicely and you’re done.

Conclusion

This was my first article here, hope it will be useful. Thanks for dedicating time to reading it, good luck.

License

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

About the Author

Niskov

Software Developer
StarLIMS
Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberjawed.ace0:36 16 May '11  
GeneralMy vote of 5 Pinmemberbtszyt4:27 22 Apr '11  
GeneralMy vote of 5 PinmemberVUnreal22:48 28 Feb '11  
GeneralMy vote of 4 Pinmemberpintel1:26 20 Jan '11  
GeneralMy vote of 3 Pinmemberdavyn3:58 4 Oct '10  
GeneralVery fine Pinmemberhappybytes4:26 15 Apr '10  
GeneralGood Work Pinmemberimgen2:43 12 Oct '09  
Generalgood work [modified] Pinmembersubai0:38 19 Oct '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 May 2008
Article Copyright 2008 by Niskov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid