How to Determine Which Office Version is Installed with VB.NET and C#

Introduction

Knowing which version of MS Office is installed on the client computer is vital. Many a programmer sees this as an afterthought, sadly. This happens because not enough research of the client's needs and abilities was done. This article will show you 4 different ways to determine the Office versions installed.

Design

Create a new project in either VB.NET or C#. You can name it something descriptive. Add four buttons to your form, and leave all the default names. You can however change the Text Properties of the buttons, as follows:

Our Design
Figure 1 - Our Design

Coding

There are numerous ways and tools at our disposal. We can use Late Binding, Assembly and even the Registry. Let us have a closer look.

Method 1 - CreateObject / CreateInstance

Add the following code:

VB.NET

    Private Sub Determine_OfficeVersion_1()
        Dim objEApp As Excel.Application 'Excel Object
        Dim strEVersion As String 'Identify Version

        objEApp = DirectCast(CreateObject("Excel.Application"), Excel.Application) 'Cast To Excel App

        Select Case objEApp.Version 'Determine Version
            Case "7.0"
                strEVersion = "95"
            Case "8.0"
                strEVersion = "97"
            Case "9.0"
                strEVersion = "2000"
            Case "10.0"
                strEVersion = "2002"
            Case "11.0"
                strEVersion = "2003"
            Case "12.0"
                strEVersion = "2007"
            Case "14.0"
                strEVersion = "2010"
        End Select

        MessageBox.Show("Excel Version: " & strEVersion) 'Display Result

        objEApp.Quit() 'Quit

        objEApp = Nothing 'Release Memory
    End Sub

C#

        private void Determine_OfficeVersion_1()
        {
            //CreateObject Not Present In C#            
            dynamic objEApp = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); //Excel object
          

            string strEVersion = null; //To identify version

            //Switch doesn't work on dynamic
            if (objEApp.Version == "7.0")
            {
                strEVersion = "95";
                
            }
            else if (objEApp.Version == "8.0")
            {
                strEVersion = "97";
            }
            else if (objEApp.Version == "9.0")
            {
                strEVersion = "2000";
            }
            else if (objEApp.Version == "10.0")
            {
                strEVersion = "2002";
            }
            else if (objEApp.Version == "11.0")
            {
                strEVersion = "2003";
            }
            else if (objEApp.Version == "12.0")
            {
                strEVersion = "2007";
            }
            else if (objEApp.Version == "14.0")
            {
                strEVersion = "2010";
            }

            MessageBox.Show("Excel Version: " + strEVersion);

            objEApp.Quit();
            //quit

            objEApp = null;
        }

Remember I spoke about Late Binding? Have a read through this article concerning late binding. Before we can run this, we need to add a Reference to the Excel object library. Click Project, Add Reference.... Select the COM tab and select your Excel version. Add the following Imports / Usings above your class name:

VB.NET

Imports Microsoft.Office.Interop

C#

using Microsoft.Office.Interop;

This allows us to make use of Office from within our code.

Our code instantiates an Excel object, then we determine its version. Based on that, we display the appropriate message. The C# code does not make use of CreateObject, as that is not available in C#. The nearest equivalent in C# is Activator.CreateInstance. This basically achieves the same thing as CreateObject. Because we do not know what will be returned by CreateInstance, we had to make the Excel object variable dynamic. The C# equivalent of Select case is switch. We cannot use switch here, because the data is dynamic / uncertain and not "fixed" such as a Boolean or character data type. Hence, the If statements.

Method 2 - The Registry

Add the following code:

    Private Sub Determine_OfficeVersion_2()
        Dim strEVersionSubKey As String = "\Excel.Application\CurVer" '/HKEY_CLASSES_ROOT/Excel.Application/Curver

        Dim strValue As String 'Value Present In Above Key
        Dim strVersion As String 'Determines Excel Version

        Dim rkVersion As RegistryKey = Nothing 'Registry Key To Determine Excel Version

        rkVersion = Registry.ClassesRoot.OpenSubKey(name:=strEVersionSubKey, writable:=False) 'Open Registry Key

        If Not rkVersion Is Nothing Then 'If Key Exists

            strValue = rkVersion.GetValue(String.Empty) 'get Value

            strValue = strValue.Substring(strValue.LastIndexOf(".") + 1) 'Store Value

            Select Case strValue 'Determine Version

                Case "7"

                    strVersion = "95"

                Case "8"

                    strVersion = "97"

                Case "9"

                    strVersion = "2000"

                Case "10"

                    strVersion = "2002"

                Case "11"

                    strVersion = "2003"

                Case "12"

                    strVersion = "2007"

                Case "14"

                    strVersion = "2010"

            End Select

            MessageBox.Show("Excel " & strVersion & " Installed!") 'Display Result

        End If

    End Sub

C#

        private void Determine_OfficeVersion_2()
        {
            
            string strEVersionSubKey = "\\Excel.Application\\CurVer"; //HKEY_CLASSES_ROOT/Excel.Application/Curver

            string strValue = null; //Value Present In Above Key
            string strVersion = null; //Determines Excel Version

            RegistryKey rkVersion = null; //Registry Key To Determine Excel Version

            rkVersion = Registry.ClassesRoot.OpenSubKey(strEVersionSubKey, false); //Open Registry Key

            if ((rkVersion != null)) //If Key Exists
            {
                strValue = (string)rkVersion.GetValue(string.Empty); //Get Value

                strValue = strValue.Substring(strValue.LastIndexOf(".") + 1); //Store Value

                switch (strValue) //Determine Version
                {
                    case "7":
                        strVersion = "95";
                        break;

                    case "8":
                        strVersion = "97";
                        break;

                    case "9":
                        strVersion = "2000";
                        break;

                    case "10":
                        strVersion = "2002";
                        break;

                    case "11":
                        strVersion = "2003";
                        break;

                    case "12":
                        strVersion = "2007";
                        break;

                    case "14":
                        strVersion = "2010";
                        break;

                }

                MessageBox.Show("Excel " + strVersion + " Installed!"); //Display Result
            }

        }

Here, we made use of the registry to obtain our needed info. Before we forget, add the Wind32 Namespace:

VB.NET

Imports Microsoft.Win32 'For registry functions

C#

using Microsoft.Win32;

OK, I said we made use of the registry, let's pause a bit here. Many people do not know what the registry is, or how to use it. I used to be scared of the registry. Why? Well, because the registry is the place where all of the settings are saved. It's like an inventory where all of the info about the programs, and the controls are stored. For more info regarding the Registry, have a read through this article I wrote a while back.

With the above code segment, we explored the HKEY_CLASSES_ROOT\Excel.Application\CurVer key. This is where the information about Excel is stored. CurVer means Current Version. Now, we have to get the information out of there and use it to determine our actual comprehensible version. The information is stored like this:

Excel.Application.12

Very few people will know that this actually means Office 2007, because it is the 12th Office to be created. With some string manipulation we extract the "12" and then do the test to see what version is present on the system. Not too difficult now is it?

Method 3 - Type

Add the following sub:

VB.NET

    Private Sub Determine_OfficeVersion_3(ByVal strVersion As String)

        Dim typOffice As Type = Type.GetTypeFromProgID(strVersion) '//Gets the type associated with the specified program identifier (ProgID). ProgID is a valid entry in the registry

        strVersion = strVersion.Substring(strVersion.LastIndexOf(".") + 1) 'Get Last 2 Characters in String

        Dim strTemp As String

        If Not typOffice Is Nothing Then 'Do We Have A Value?

            Select Case strVersion 'Determine Version

                Case "7"
                    strTemp = "95"

                Case "8"
                    strTemp = "97"

                Case "9"
                    strTemp = "2000"

                Case "10"
                    strTemp = "2002"

                Case "11"
                    strTemp = "2003"

                Case "12"
                    strTemp = "2007"

                Case "14"
                    strTemp = "2010"

            End Select

            MessageBox.Show("Excel " & strTemp & " Installed") 'Display Result

        End If

    End Sub

C#

        private void Determine_OfficeVersion_3(string strVersion)
        {
            Type typOffice = Type.GetTypeFromProgID(strVersion); //Gets the type associated with the specified program identifier (ProgID). ProgID is a valid entry in the registry

            strVersion = strVersion.Substring(strVersion.LastIndexOf(".") + 1); //Get Last 2 Characters in String

            string strTemp = null; //Initialize

            if ((typOffice != null)) //Do We Have A Value?

            {
                
                switch (strVersion) //Determine Version
                {

                    case "7":

                        strTemp = "95";
                        break;

                    case "8":
                        strTemp = "97";
                        break;

                    case "9":
                        strTemp = "2000";
                        break;

                    case "10":
                        strTemp = "2002";
                        break;

                    case "11":
                        strTemp = "2003";
                        break;

                    case "12":
                        strTemp = "2007";
                        break;

                    case "14":
                        strTemp = "2010";
                        break;

                }

                MessageBox.Show("Excel " + strTemp + " Installed"); //Display Result
            }


        }

Many familiar terms in here. What is new is the little word Type. This gets the type associated with the specified program identifier (ProgID). OK, what does this mean? It means it checks the Type associated with the Program ID you supply. This Program ID must be a valid registry entry such as Excel.Application.12 back in Method 2. Once we have established that what was supplied was indeed valid, we do some string manipulation again and obtain the version again.

Method 4 - Assembly

Add the last sub:

VB.NET

    Private Sub Determine_OfficeVersion_4()

        'Assembly we're looking for
        Dim strAssemblyOff2007 As String = "Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"

        Try ' Use Try Instead of IF, Because On Error, Program Will Crash

            Dim xslExcelAssembly As [Assembly] = [Assembly].Load(strAssemblyOff2007) 'Load Assembly

            If Not xslExcelAssembly Is Nothing Then 'If Exists

                MessageBox.Show("Excel 2007 Installed") 'Success

            End If

        Catch ex As Exception

            MessageBox.Show("Not Installed") 'Don't Exist

        End Try

    End Sub

C#

        private void Determine_OfficeVersion_4()
        {
            //Assembly we're looking for
            string strAssemblyOff2007 = "Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";

            try // Use Try Instead of IF, Because On Error, Program Will Crash

            {

                Assembly xslExcelAssembly = Assembly.Load(strAssemblyOff2007); //Load Assembly

                if ((xslExcelAssembly != null)) //If Exists

                {

                    MessageBox.Show("Excel 2007 Installed"); //Success!

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("Not Installed"); //Don't Exist

            }

        }

Add the Reflection Namespace:

VB.NET

Imports System.Reflection

C#

using System.Reflection;

Assembly is part of the Reflection namespace. We specify the exact assembly we're looking for, and try to load it. If the Load was successful, we display a message saying that the program we're looking for has been found. If the Load was not successful, an error will be generated.

All that is left now is to call these subs from our buttons. Add the next code:

VB.NET

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Determine_OfficeVersion_1()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Determine_OfficeVersion_2()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Determine_OfficeVersion_3("Excel.Application.12")

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Determine_OfficeVersion_4()

    End Sub

C#

        private void Button1_Click(object sender, EventArgs e)
        {
            
            Determine_OfficeVersion_1();

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            
            Determine_OfficeVersion_2();

        }

        private void Button3_Click(object sender, EventArgs e)
        {
            
            Determine_OfficeVersion_3("Excel.Application.12");

        }

        private void Button4_Click(object sender, EventArgs e)
        {
            
            Determine_OfficeVersion_4();

        }

If you run this application now, you will be able to click on all 4 buttons and get the same result. Just a note. I used Excel 2007 with my program. You can just modify at the various places the Office version or Office application you're looking for. I am attaching my programs here, for you to experiment with.

Conclusion

There! Now you have 4 ways to determine if Office is installed on the client computer. Do not forget to use them! I hope you have benefited from this article. Until next time, cheers!



Related Articles

Downloads

Comments

  • Good

    Posted by yaqui on 06/30/2014 03:35am

    Thank you very much! I was looking for that!

    Reply
  • na

    Posted by alpewb on 01/30/2013 05:42pm

    how about doing same for remote pc ? for Win7

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • As a development and deployment platform, RHEL offers an efficient, scalable, and robust operating environment with certified security and flexible deployment options in physical and virtualized environments. To assess and quantify the business benefits of RHEL, IDC recently conducted in-depth interviews with IT staff members of 21 companies using RHEL servers. The organizations represent a broad range of industries and have an average of 22,700 employees. RHEL servers accounted for 23% of the servers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds