Results 1 to 4 of 4

Thread: [FAQ's: OD] How do I programmatically determine the version of an Office App?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    [FAQ's: OD] How do I programmatically determine the version of an Office App?

    When writting programs that need to support multiple versions of an Office app you need a way to evaluate the installed app. There are only two methods to obtain the version/build information for all Office apps. Once you have the version info you can branch out and perform vertain functions that are only supported in that version or set a flag variable to use throughout your app to handle executing certain lines of code or not.


    1.) The code in Command1_Click procedure is only for Outlook and InfoPath applications. In these two apps the .Version information contains the Major, Minor, Revision and Build (14.0.0.6359).


    2.) The code in command2_Click procedure covers the rest of the Office applications. The .Version information only contains the Major and Minor numbers (14.0).


    Visual Basic 6.0 Early Binding Code Example:

    VB Code:
    1. Option Explicit
    2. 'Add a reference to your desired office app
    3. 'MS [i][inset app name here][/i] xx.0 Object Library
    4. Private Sub Command1_Click()
    5. 'Format for Outlook and InfoPath (Outlook shown)
    6. 'Requires trimming of the build info
    7.     Dim oApp As Outlook.Application
    8.     Dim sBuild As String
    9.     Dim sVersion As String
    10.     Set oApp = New Outlook.Application
    11.     'Get major build only to make it easier to determine version
    12.     sBuild = Left$(oApp.Version, InStr(1, oApp.Version, ".") + 1)
    13.     Select Case sBuild
    14.         Case "7.0"
    15.             sVersion = "97"   'Outlook
    16.         Case "8.0"
    17.             sVersion = "98"   'Outlook
    18.         Case "9.0"
    19.             sVersion = "2000" 'Outlook
    20.         Case "10.0"
    21.             sVersion = "2002" 'Outlook
    22.         Case "11.0"
    23.             sVersion = "2003" 'Outlook & InfoPath
    24.         Case "12.0"
    25.             sVersion = "2007" 'Outlook & InfoPath
    26.         Case "14.0"
    27.             sVersion = "2010" 'Outlook
    28.         Case "15.0"
    29.             sVersion = "2013" 'Outlook
    30.         Case "16.0"
    31.             sVersion = "2016" 'Outlook
    32.         Case Else
    33.             sVersion = "Undetermined!"
    34.     End Select
    35.     MsgBox "Outlook Build: " & oApp.Version & vbNewLine & "Outlook Version: " & sVersion, _
    36.     vbOKOnly + vbInformation, "Office Version"
    37.     oApp.Quit
    38.     Set oApp = Nothing
    39. End Sub
    40.  
    41. Private Sub Command2_Click()
    42. 'Format for most other Office Apps
    43. 'MS Access used for this example
    44.     Dim oApp As Access.Application
    45.     Dim sVersion As String
    46.     Set oApp = New Access.Application
    47.     Select Case oApp.Version
    48.         Case "7.0"
    49.             sVersion = "95"
    50.         Case "8.0"
    51.             sVersion = "97"
    52.         Case "9.0"
    53.             sVersion = "2000"
    54.         Case "10.0"
    55.             sVersion = "2002"
    56.         Case "11.0"
    57.             sVersion = "2003"
    58.         Case "12.0"
    59.             sVersion = "2007"
    60.         Case "14.0"
    61.             sVersion = "2010"
    62.         Case "15.0"
    63.             sVersion = "2013"
    64.         Case "16.0"
    65.             sVersion = "2016"
    66.         Case else
    67.             sVersion = "Undetermined!"
    68.     End Select
    69.     MsgBox "Outlook Build: " & oApp.Version & vbNewLine & "Outlook Version: " & sVersion, _
    70.     vbOKOnly + vbInformation, "Office Version"
    71.     oApp.Quit
    72.     Set oApp = Nothing
    73. End Sub

    Visual Basic 6.0 Late Binding Code Example:
    VB Code:
    1. Option Explicit
    2. 'Excel example being used
    3. Private Sub Form_Load()
    4.  
    5.     On Error GoTo MyError
    6.  
    7.     Dim oApp As Object
    8.     Dim sVersion As String
    9.     Set oApp = GetObject(, "Excel.Application")
    10.     If TypeName(oApp) = "Nothing" Then
    11.         Set oApp = CreateObject("Excel.Application")
    12.     End If
    13.     Select Case Left$(oApp.Version, InStr(1, oApp.Version, ".") + 1)
    14.         Case "8.0"
    15.             sVersion = "97"  
    16.         Case "9.0"
    17.             sVersion = "2000"
    18.         Case "10.0"
    19.             sVersion = "2002"
    20.         Case "11.0"
    21.             sVersion = "2003"
    22.         Case "12.0"
    23.             sVersion = "2007"
    24.         Case "14.0"
    25.             sVersion = "2010"
    26.         Case "15.0"
    27.             sVersion = "2013"
    28.         Case "16.0"
    29.             sVersion = "2016"
    30.         Case else
    31.             sVersion = "Undetermined!"
    32.     End Select
    33.     MsgBox "Excel version: " & sVersion
    34.     Exit Sub
    35. MyError:
    36.     If Err.Number = 429 Then
    37.         Resume Next
    38.     Else
    39.         MsgBox Err.Number & " - " & Err.Description
    40.     End If
    41. End Sub
    Last edited by RobDog888; Jul 3rd, 2017 at 10:36 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I programmatically determine the version of an Office App?

    VB.NET 2003 Code Example:
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Add a reference to your desired Office App -
    4. 'MS [inset app name here] xx.0 Object Library
    5. 'MS Word xx.0 Object Library
    6. 'MS Word used in this code example
    7. 'Add the imports statement for the .Interop class (PIA)
    8. Imports Microsoft.Office.Interop
    9.  
    10. Public Class Form1
    11.  
    12.     Inherits System.Windows.Forms.Form
    13.  
    14.     "Windows Form Designer generated code"
    15.  
    16.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    17.         Dim oApp As Word.Application
    18.         Dim sVersion As String
    19.         oApp = DirectCast(CreateObject("Word.Application"), Word.Application)
    20.         Select Case oApp.Version
    21.             Case "7.0"
    22.                 sVersion = "95"
    23.             Case "8.0"
    24.                 sVersion = "97"
    25.             Case "9.0"
    26.                 sVersion = "2000"
    27.             Case "10.0"
    28.                 sVersion = "2002"
    29.             Case "11.0"
    30.                 sVersion = "2003"
    31.             Case "12.0"
    32.                 sVersion = "2007"
    33.             Case "14.0"
    34.                 sVersion = "2010"
    35.             Case "15.0"
    36.                 sVersion = "2013"
    37.             Case "16.0"
    38.                 sVersion = "2016"
    39.             Case else
    40.                 sVersion = "Undetermined!"
    41.         End Select
    42.         MessageBox.Show("Word Build: " & oApp.Version & Environment.NewLine & "Word Version: " & sVersion,  _
    43.         "Office Version", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    44.         oApp.Quit(False)
    45.         oApp = Nothing
    46.     End Sub
    47.  
    48. End Class
    Last edited by RobDog888; Jul 3rd, 2017 at 10:33 AM. Reason: Fix code tags from recent upgrade
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I programmatically determine the version of an Office App?

    C# 2003 Code Example:
    Code:
    'Add a reference to your desired Office App -
    'MS [inset app name here] xx.0 Object Library
    'MS Word xx.0 Object Library
    'MS Word used in this code example
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Microsoft.Office.Interop;
    
    namespace CSharp___Word_Version
    {
    
        public class Form1 : System.Windows.Forms.Form
            {
            private System.Windows.Forms.Button button1;
    
            private System.ComponentModel.Container components = null;
    
            public Form1()
            {
                /// <summary>
                /// Required for Windows Form Designer support
                /// <summary>
                InitializeComponent();
            }
            "Windows Form Designer generated code"
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }
            private void button1_Click(object sender, System.EventArgs e)
            {
                string sVersion = string.Empty;
                Microsoft.Office.Interop.Word.Application oApp = new Microsoft.Office.Interop.Word.Application();
                oApp.Visible = false;
                switch (oApp.Version.ToString())
                {
                    case "7.0":
                        sVersion = "95";
                        break;
                    case "8.0":
                        sVersion = "97";
                        break;
                    case "9.0":
                        sVersion = "2000";
                        break;
                    case "10.0":
                        sVersion = "2002";
                        break;
                    case "11.0":
                        sVersion = "2003";
                        break;
                    case "12.0":
                        sVersion = "2007";
                        break;
                    case "14.0":
                        sVersion = "2010";
                        break;
                    case "15.0":
                        sVersion = "2013";
                        break;
                    case "16.0":
                        sVersion = "2016";
                        break;
                    default:
                        sVersion = "Undetermined!";
                        break;
                }
                MessageBox.Show("Word Build: " + oApp.Version + Environment.NewLine + "Word Version: " + sVersion,
                "Office Version",MessageBoxButtons.OK, MessageBoxIcon.Information);
                object a = (object) false;
                object b = (object) 1;
                oApp.Quit(ref a, ref b, ref a);
                oApp = null;
            }
        }
    }
    Last edited by RobDog888; Jul 3rd, 2017 at 10:35 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Re: [FAQ's: OD] How do I programmatically determine the version of an Office App?

    Thank you dear RobDog888 it is very helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width