[VBA] How to Get OS and Excel Version Information (Compatibility Measures)

目次

Introduction

When you share your VBA macros with others, they might not work as intended depending on the user’s PC environment (e.g., Windows 10 vs. 11, or 32-bit vs. 64-bit Excel).

Using VBA, you can easily retrieve the OS information of the PC running the macro, as well as the version information of Excel itself. This allows you to branch processing based on the environment or record logs for troubleshooting, enabling you to create more robust and user-friendly tools.

In this article, I will explain two different methods for getting OS and Excel version information using VBA.

1. How to Get OS Version Information (WMI)

To get detailed information about the Windows OS, utilizing WMI (Windows Management Instrumentation) is the most powerful method. From VBA, you access WMI through the WbemScripting.SWbemLocator object.

Sample Code

' Get OS information using WMI
Sub GetOperatingSystemInfo()
    Dim wmiService As Object
    Dim osInfoSet As Object
    Dim os As Object
    
    ' Connect to the WMI service
    Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
    
    ' Query information from the Win32_OperatingSystem class
    Set osInfoSet = wmiService.ExecQuery("Select * From Win32_OperatingSystem")
    
    ' Display the retrieved information
    For Each os In osInfoSet
        MsgBox "OS Name: " & os.Caption & vbCrLf & _
               "Version: " & os.Version & vbCrLf & _
               "Architecture: " & os.OSArchitecture, _
               vbInformation, "OS Information"
    Next
End Sub

Code Explanation

  • GetObject("winmgmts:..."): This is the command to connect to the WMI service.
  • ExecQuery("Select * From Win32_OperatingSystem"): This executes a query to WMI asking for “all information related to the OS (Win32_OperatingSystem class).”
  • os.Caption, os.Version, os.OSArchitecture: These properties retrieve specific details like the OS name (e.g., Microsoft Windows 11 Pro), version number, and architecture (e.g., 64-bit) from the object returned by the query.

2. How to Get Excel Version Information (Application Object)

You can easily get version information for Excel itself from properties belonging to the VBA Application object.

Sample Code

' Get Excel version information from the Application object
Sub GetExcelInfo()
    With Application
        MsgBox "Excel Version: " & .Version & vbCrLf & _
               "Build Number: " & .Build & vbCrLf & _
               "OS Info (As seen by Excel): " & .OperatingSystem, _
               vbInformation, "Excel Information"
    End With
End Sub

Code Explanation

  • .Version: Returns the Excel version number as a string (e.g., “16.0” indicates Excel 2016 or later).
  • .Build: Returns the detailed build number of Excel.
  • .OperatingSystem: Returns the name and version of the OS as recognized by Excel (e.g., “Windows (64-bit) NT 10.00”).

Summary

In this article, I explained how to get OS and Excel version information using VBA.

  1. If you need detailed OS information, use WMI to retrieve it from Win32_OperatingSystem.
  2. If you only need Excel version information, referring to the Application object properties (like .Version) is the easiest way.

Recording this information in a macro log file allows you to accurately understand the environment where a problem occurred if a user contacts you, leading to smoother support.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次