Click here to Skip to main content
Click here to Skip to main content

Microsoft Office Version Detector

By , 8 Nov 2012
 

Introduction 

While short on time and after a long search for a way to get Microsoft Office application information, all or specific (ie. word, excell, access etc.).

I found a few great articles here at codeproject.com 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 nor only way.

Also, there are many directions you can take this, many changes, additions to be made, output to a grid, faster more efficent 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 http://www.codeproject.com/Articles/16622/Microsoft-Office-Version-Detector

and to Niskof for his article found here at http://www.codeproject.com/Articles/26520/Getting-Office-s-Version.

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

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

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.

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 

Code follows:

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 h ttp://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<span class="Apple-tab-span" style="white-space: pre; ">	</span>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\
<span class="Apple-tab-span" style="white-space: pre; ">	</span>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 all. 

At some point I may need to rework this into a C# class in 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#. 

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)

About the Author

aalvarez13
United States United States
Member
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalwait for your c# projectmemberEYUANatQQdotCOM8 Nov '12 - 2:48 
the code is so clear to understand.
i am wait for your C# project,
or i will try to translate it to c#,
NewsRe: wait for your c# projectmemberaalvarez138 Nov '12 - 8:29 
Thank you for your comment. I actually updated this article today and posted the C# code at the bottom. Look for the update to show soon as the article update is currently pending approval.
QuestionYou need to turn Option Strict Onmemberkevininstructor29 Oct '12 - 4:27 
Hello, I would highly suggest turning Option Strict On and correct issues ranging from a function that does not specify it's return type to casting issues. In this day and age there is no reason to write code without Option Strict On for this type of code.
Kevin S. Gallagher
Programming is an art form that fights back

AnswerRe: You need to turn Option Strict On [modified]memberaalvarez1329 Oct '12 - 8:49 
Excellent suggestion and I absolutely agree. Like I've said before, there are many different ways you can take this code to suit your purposes, many improvements that can/should be made, but at the moment this was our quickest solution.
I did however, find some time this morning and made the changes as you suggested.
Thank you kindly Kevininstructor.

modified 30 Oct '12 - 14:36.

SuggestionOffice 2010?memberGeekForChrist27 Oct '12 - 3:10 
I saw in the code that you only went up to Office 2007.
It seems like it would be simple to add so I just wanted to mention it.
 
Overall, very good.Thumbs Up | :thumbsup:
GeneralRe: Office 2010?memberaalvarez1329 Oct '12 - 8:52 
A great suggestion as well. At the time, we only needed to check for the previous versions. If i have time I'll update the code. Thank you.
GeneralMy vote of 5memberfredatcodeproject26 Oct '12 - 2:40 
pretty good

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 8 Nov 2012
Article Copyright 2012 by aalvarez13
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid