Click here to Skip to main content
15,867,568 members
Articles / Productivity Apps and Services / Microsoft Office
Alternative
Article

Microsoft Office Version Detector

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
8 Nov 2012CPOL2 min read 33.2K   22   8
This is an alternative for "Microsoft Office Version Detector".

Introduction

While short on time and after a long search for a way to get Microsoft Office application information, all or specific (i.e., Word, Excel, Access etc.), I found a few great articles here on Codeproject and translated from C# / combined them to procure the data I needed. Although I am not the author of the code, I did manage to make it work for my particular case. There are several other ways to go about this and this by no means is the best or only way.

Also, there are many directions you can take this, many changes, additions to be made, output to a grid, faster, more efficient, etc. Hopefully, this will help anyone who is lost or is having a difficult time finding help on doing something similar.

Background

This is my first article here on Codeproject and the basis for this comes from an article from Warren Stevens titled Microsoft Office Version Detector.

Full credit for this goes to Warren Stevens and Daneil Leykauf in the article and comments found in this article and to Niskof for his article.

Again, this is not my original work but I found these articles extremely helpful in completing my task.

I needed to determine the details of an MS Office Application such as the following:

  1. Full application EXE name
  2. Original filename
  3. Full application EXE path
  4. File version number
  5. Language

Using the Code

Usage is fairly straight forward. For testing purposes, I put a call in the forms load event.

VB
Private Sub frmOfficeVersion_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ListAllOfficeVersions() 'to list all of the apps
    ListSpecificOfficeVersion(MSOfficeApp.Word_Application)
End Sub 

The code follows:

VB.NET
Option Strict On    '<--thanks for the reminder kevininstructor

Imports Microsoft.Win32
Imports System.IO

Public Class frmOfficeVersion

    ''' Possible Office apps.
    Enum MSOfficeApp
        Access_Application
        Excel_Application
        Outlook_Application
        PowerPoint_Application
        Word_Application
        FrontPage_Application
    End Enum

    ''' Possible versions
    Enum Version
        Version2007 = 12
        Version2003 = 11
        Version2002 = 10
        Version2000 = 9
        Version97 = 8
        Version95 = 7
    End Enum

    ''' Lists all available MS Office apps and version in console window
    Public Sub ListAllOfficeVersions()
        'created by Daneil Leykauf from codeproject.com
        'under this article http://www.codeproject.com/Articles/16622/Microsoft-Office-Version-Detector
        'at this profile h ttp://www.codeproject.com/script/Membership/View.aspx?mid=4804015
        'modified by aalvarez of dane-elec 10/24/12
        Dim strApp As String = Nothing

        For Each s As String In [Enum].GetNames(GetType(MSOfficeApp))
            If Not IsNothing(GetComponentPath(CType([Enum].Parse(GetType(MSOfficeApp), s), _
                 MSOfficeApp), True)) Then
                strApp = GetComponentPath(CType([Enum].Parse(GetType(MSOfficeApp), s), _
                   MSOfficeApp), True).ToString
            Else
                strApp = "Not Found Err."
            End If

            If File.Exists((strApp).ToUpper) Then
                Dim _fileVersion As FileVersionInfo = FileVersionInfo.GetVersionInfo(strApp.ToUpper)
                Debug.Print(s & vbTab & GetVersionsString(CType([Enum].Parse_
                                   (GetType(MSOfficeApp), s), MSOfficeApp)))
                Debug.Print("App Path Full = " & (strApp).ToUpper)
                Debug.Print("App Exists: " & _fileVersion.ToString)
            Else
                Debug.Print(s & vbTab & GetVersionsString(CType([Enum].Parse_
                                         (GetType(MSOfficeApp), s), MSOfficeApp)))
            End If

            Debug.Print(vbCrLf & "=================" & vbCrLf)

        Next
    End Sub

    Public Sub ListSpecificOfficeVersion(ByVal MsApplication As MSOfficeApp)
        Debug.Print(GetComponentPath(MsApplication))
        Dim strApp As String = Nothing
        If Not IsNothing(GetComponentPath(MsApplication, True)) Then
            strApp = GetComponentPath(MsApplication, True).ToString
        Else
            strApp = "Not Found Err."
        End If

        If File.Exists((strApp).ToUpper) Then
            Dim _fileVersion As FileVersionInfo = FileVersionInfo.GetVersionInfo(strApp.ToUpper)
            Debug.Print(vbTab & GetVersionsString(MsApplication))
            Debug.Print("App Path Full = " & (strApp).ToUpper)
            Debug.Print("App Exists: " & _fileVersion.ToString)
        Else
            Debug.Print(vbTab & GetVersionsString(MsApplication))
        End If

        Debug.Print(vbCrLf & "=================" & vbCrLf)

    End Sub

    ''' Returns version number as integer
    ''' Value is 0 if no version could be detected
    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 = CStr(regKey.GetValue("", Nothing, RegistryValueOptions.None))
        Debug.Print(strV)
        regKey.Close()

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

    ''' Returns the version string
    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)
        'Debug.Print(regKey.ToString)
        If IsNothing(regKey) Then Return "No version detected."
        Dim strV As String = CStr(regKey.GetValue("", Nothing, RegistryValueOptions.None))
        'Debug.Print(strV)
        regKey.Close()

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

    Private Function GetComponentPath(ByVal _component As MSOfficeApp, _
                    Optional ByVal blnFullPath As Boolean = False) As String
        'code in c# by Niskov from codeproject.com
        'http://www.codeproject.com/script/Membership/View.aspx?mid=4840737
        '
        'translated and modified to vb.net by aalvarez of Dane-Elec
        '=======================================================

        Const RegKey As String = "Software\Microsoft\Windows\CurrentVersion\App Paths"
        Dim toReturn As String = Nothing
        Dim _key As String = Nothing

        Select Case _component
            Case MSOfficeApp.Word_Application
                _key = "winword.exe"

            Case MSOfficeApp.Excel_Application
                _key = "excel.exe"

            Case MSOfficeApp.PowerPoint_Application
                _key = "powerpnt.exe"

            Case MSOfficeApp.Outlook_Application
                _key = "outlook.exe"

            Case MSOfficeApp.Access_Application
                _key = "MSACCESS.exe"

            Case MSOfficeApp.FrontPage_Application
                _key = "FrontPg.exe"

        End Select

        _key = _key.ToUpper

        'looks inside CURRENT_USER:
        Dim _mainKey As RegistryKey = Registry.CurrentUser

        Try
            _mainKey = _mainKey.OpenSubKey(RegKey & "\" & _key, False)
            If _mainKey IsNot Nothing Then
                toReturn = _mainKey.GetValue(String.Empty).ToString()
                If blnFullPath Then toReturn = toReturn & _key
            End If
        Catch
        End Try

        'if not found, looks inside LOCAL_MACHINE:
        _mainKey = Registry.LocalMachine
        If String.IsNullOrEmpty(toReturn) Then
            Try
                _mainKey = _mainKey.OpenSubKey(RegKey & "\" & _key, False)
                If _mainKey IsNot Nothing Then
                    toReturn = _mainKey.GetValue("Path").ToString()
                    If blnFullPath Then toReturn = toReturn & _key
                End If
            Catch
            End Try
        End If

        'closing the handle:
        If _mainKey IsNot Nothing Then
            _mainKey.Close()
        End If

        Return toReturn

    End Function

    Private Sub frmOfficeVersion_Load(sender As System.Object, _
                           e As System.EventArgs) Handles MyBase.Load
        'ListAllOfficeVersions()
        ListSpecificOfficeVersion(MSOfficeApp.Word_Application)
    End Sub

End Class

Which then returns an output similar to the following:

=================
Word_Application Version2003
App Path Full = C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\WINWORD.EXE
App Exists: File:             C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\WINWORD.EXE
InternalName:     WinWord
OriginalFilename: WinWord.exe
FileVersion:      11.0.8345
FileDescription:  Microsoft Office Word
Product:          Microsoft Office 2003
ProductVersion:   11.0.8345
Debug:            False
Patched:          False
PreRelease:       False
PrivateBuild:     False
SpecialBuild:     False
Language:         Language Neutral

=================
FrontPage_Application No version detected.
=================
C:\Program Files\Microsoft Office\OFFICE11\
C:\Program Files\Microsoft Office\OFFICE11\Version2003
App Path Full = C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\WINWORD.EXE
App Exists: File:             C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\WINWORD.EXE
InternalName:     WinWord
OriginalFilename: WinWord.exe
FileVersion:      11.0.8345
FileDescription:  Microsoft Office Word
Product:          Microsoft Office 2003
ProductVersion:   11.0.8345
Debug:            False
Patched:          False
PreRelease:       False
PrivateBuild:     False
SpecialBuild:     False
Language:         Language Neutral

Points of Interest

Once again, full credit goes to Warren Stevens, Daneil Leykauf and Niskof here at codeproject.com.

Thank you for reading!

At some point, I may need to rework this into a C# class for which I will post an update here.

History

Again this is my first article. Thanks to all the helpful people here at codeproject.com.

C# Update

Thank you everyone for you suggestions. Here is a quick conversion to C#.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Collections;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;

namespace csOfficeVersion
{
    public partial class frmOfficeVersion : Form
    {
        public frmOfficeVersion()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //Console.WriteLine ("This is a test");
            ListAllOfficeVersions();
        }

        //BEGIN===========================>
        /// Possible Office apps.
        public enum MSOfficeApp
        {
            Access_Application,
            Excel_Application,
            Outlook_Application,
            PowerPoint_Application,
            Word_Application,
            FrontPage_Application
        }
        /// Possible versions
        public enum Version
        {
            Version2007 = 12,
            Version2003 = 11,
            Version2002 = 10,
            Version2000 = 9,
            Version97 = 8,
            Version95 = 7
        }

        /// Lists all available MS Office apps and version in console window
        public void ListAllOfficeVersions()
        {
            string strApp = null;
            foreach (string s in Enum.GetNames(typeof(MSOfficeApp)))
            {
                if ((GetComponentPath((MSOfficeApp)Enum.Parse_
                              (typeof(MSOfficeApp), s), true) != null))
                {
                    strApp = GetComponentPath((MSOfficeApp)Enum.Parse_
                              (typeof(MSOfficeApp), s), true).ToString();
                }
                else
                {
                    strApp = "Not Found Err.";
                }
                if (File.Exists((strApp).ToUpper()))
                {
                    FileVersionInfo _fileVersion = _
                            FileVersionInfo.GetVersionInfo(strApp.ToUpper());
                    Debug.Print(s + "\t" + _
                            GetVersionsString((MSOfficeApp)Enum.Parse(typeof(MSOfficeApp), s)));
                    Debug.Print("App Path Full = " + (strApp).ToUpper());
                    Debug.Print("App Exists: " + _fileVersion.ToString());
                }
                else
                {
                    Debug.Print(s + "\t" + _
                      GetVersionsString((MSOfficeApp)Enum.Parse(typeof(MSOfficeApp), s)));
                }
                Debug.Print("\n" + "=================" + "\n");
            }
        }
        public void ListSpecificOfficeVersion(MSOfficeApp MsApplication)
        {
            Debug.Print(GetComponentPath(MsApplication));
            string strApp = null;
            if ((GetComponentPath(MsApplication, true) != null))
            {
                strApp = GetComponentPath(MsApplication, true).ToString();
            }
            else
            {
                strApp = "Not Found Err.";
            }

            if (File.Exists((strApp).ToUpper()))
            {
                FileVersionInfo _fileVersion = FileVersionInfo.GetVersionInfo(strApp.ToUpper());
                Debug.Print("\t" + GetVersionsString(MsApplication));
                Debug.Print("App Path Full = " + (strApp).ToUpper());
                Debug.Print("App Exists: " + _fileVersion.ToString());
            }
            else
            {
                Debug.Print("\t" + GetVersionsString(MsApplication));
            }
            Debug.Print("\n" + "=================" + "\n");
        }

        /// Returns version number as integer
        /// Value is 0 if no version could be detected
        public static int GetVersionsID(MSOfficeApp app)
        {
            string strProgID = Enum.GetName(typeof(MSOfficeApp), app);
            strProgID = strProgID.Replace("_", ".");
            RegistryKey regKey = null;
            regKey = Registry.LocalMachine.OpenSubKey_
                     ("Software\\Classes\\" + strProgID + "\\CurVer", false);
            if ((regKey == null))
                return 0;
            string strV = Convert.ToString(regKey.GetValue("", null, RegistryValueOptions.None));
            Debug.Print(strV);
            regKey.Close();
            strV = strV.Replace(strProgID, "");
            strV = strV.Replace(".", "");

            return Convert.ToInt32(strV);
        }

        /// Returns the version string
        public static string GetVersionsString(MSOfficeApp app)
        {
            string strProgID = Enum.GetName(typeof(MSOfficeApp), app);
            strProgID = strProgID.Replace("_", ".");
            RegistryKey regKey = null;
            regKey = Registry.LocalMachine.OpenSubKey_
                    ("Software\\Classes\\" + strProgID + "\\CurVer", false);
            //Debug.Print(regKey.ToString)
            if ((regKey == null))
                return "No version detected.";
            string strV = Convert.ToString(regKey.GetValue("", null, RegistryValueOptions.None));
            //Debug.Print(strV)
            regKey.Close();
            strV = strV.Replace(strProgID, "");
            strV = strV.Replace(".", "");
            return Enum.GetName(typeof(Version), Convert.ToInt32(strV));
        }

        private string GetComponentPath(MSOfficeApp _component, bool blnFullPath = false)
        {
            //code in c# by Niskov from codeproject.com
            //http://www.codeproject.com/script/Membership/View.aspx?mid=4840737
            //
            //translated and modified to vb.net by aalvarez of Dane-Elec
            //=======================================================
            const string RegKey = "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths";
            string toReturn = null;
            string _key = null;
            switch (_component)
            {
                case MSOfficeApp.Word_Application:
                    _key = "winword.exe";
                    break;
                case MSOfficeApp.Excel_Application:
                    _key = "excel.exe";
                    break;
                case MSOfficeApp.PowerPoint_Application:
                    _key = "powerpnt.exe";
                    break;
                case MSOfficeApp.Outlook_Application:
                    _key = "outlook.exe";
                    break;
                case MSOfficeApp.Access_Application:
                    _key = "MSACCESS.exe";
                    break;
                case MSOfficeApp.FrontPage_Application:
                    _key = "FrontPg.exe";
                    break;
            }
            _key = _key.ToUpper();
            //looks inside CURRENT_USER:
            RegistryKey _mainKey = Registry.CurrentUser;
            try
            {
                _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
                if (_mainKey != null)
                {
                    toReturn = _mainKey.GetValue(string.Empty).ToString();
                    if (blnFullPath)
                        toReturn = toReturn + _key;
                }
            }
            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("Path").ToString();
                        if (blnFullPath)
                            toReturn = toReturn + _key;
                    }
                }
                catch
                {
                }
            }
            //closing the handle:
            if (_mainKey != null)
            {
                _mainKey.Close();
            }
            return toReturn;
        }
        //END=============================>
    }}

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCan I detect MS Office 32 - 64 bit version and Language? Pin
abdul subhan mohammed23-Jan-19 20:18
professionalabdul subhan mohammed23-Jan-19 20:18 
Generalwait for your c# project Pin
EYUANatQQdotCOM8-Nov-12 2:48
EYUANatQQdotCOM8-Nov-12 2:48 
NewsRe: wait for your c# project Pin
aalvarez138-Nov-12 8:29
aalvarez138-Nov-12 8:29 
QuestionYou need to turn Option Strict On Pin
karenpayne29-Oct-12 4:27
karenpayne29-Oct-12 4:27 
AnswerRe: You need to turn Option Strict On Pin
aalvarez1329-Oct-12 8:49
aalvarez1329-Oct-12 8:49 
SuggestionOffice 2010? Pin
Casey Sheridan27-Oct-12 3:10
professionalCasey Sheridan27-Oct-12 3:10 
GeneralRe: Office 2010? Pin
aalvarez1329-Oct-12 8:52
aalvarez1329-Oct-12 8:52 
GeneralMy vote of 5 Pin
fredatcodeproject26-Oct-12 2:40
professionalfredatcodeproject26-Oct-12 2:40 

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.