5

I have an Excel workbook used by several other users. I've discovered that a few of the functions work differently on different windows OS. My workbook needs to react to the different windows.

In the past,

b = oWsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName")"^

would tell me the version, ie "Windows 10 Enterprise". Actually it's a little weird because when I look in the registry it says "Windows 10 Pro"...where does the "Enterprise" come from?

Anyway, when I run the same command on Win 11, it still says "Windows 10 Pro".

How can I find out if the OS on someone else's PC is Win 10 or Win 11?

As described in the details.

Progman
  • 16,827
  • 6
  • 33
  • 48

3 Answers3

3

You can find out a lot by using WMI. It is quite cryptic and doing in VBA doesn't make it less so.

Function IsWindows11() As Boolean
    Dim WMIService As Object
    Dim OsInfo As Object
    Dim Entry As Object
    Set WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set OsInfo = WMIService.ExecQuery("Select * from Win32_OperatingSystem")
    For Each Entry In OsInfo
        If InStr(Entry.Name, "Microsoft Windows 11") > 0 Then
            IsWindows11 = True
            Exit Function
        End If
    Next
    IsWindows11 = False
End Function

If you want to understand why this works, read up on WMI. You can do similar things in Powershell with a command like this

Get-ComputerInfo  | select OsName

but then you'll have to call that and pick up the answer.

Sam
  • 5,424
  • 1
  • 18
  • 33
1

The OP stated here, that the link, VBA to find Windows Version, posted in a comment by Siddharth Rout, answered the question. Following is the code from that link.

Code from post author

'FUNCTION TO DETERMINE WINDOWS VERSION NUMBER

Option Compare Database
Option Explicit

Private Type OSVERSIONINFO
  dwOSVersionInfoSize As Long
  dwMajorVersion As Long
  dwMinorVersion As Long
  dwBuildNumber As Long
  dwPlatformId As Long
  szCSDVersion As String * 128
End Type

Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer

Public Function getVersion() As String

  Dim osinfo As OSVERSIONINFO
  Dim retvalue As Integer

  osinfo.dwOSVersionInfoSize = 148
  osinfo.szCSDVersion = Space$(128)
  retvalue = GetVersionExA(osinfo)

  getVersion = "Version is " & osinfo.dwMajorVersion & "." & osinfo.dwMinorVersion

End Function

Code in a reply

'---------------------------------------------------------------------------------------
' Procedure : getOperatingSystem
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return the active OS details
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-Sep-27                 Initial Release
'---------------------------------------------------------------------------------------
Public Function getOperatingSystem()
    Dim localHost       As String
    Dim objWMIService   As Variant
    Dim colOperatingSystems As Variant
    Dim objOperatingSystem As Variant

    On Error GoTo Error_Handler

    localHost = "." 'Technically could be run against remote computers, if allowed
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & localHost & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem In colOperatingSystems
        getOperatingSystem = objOperatingSystem.Caption & " " & objOperatingSystem.Version
        Exit Function
    Next

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: getOperatingSystem" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

Debug.Print Application.OperatingSystem

(but as @Sam says it does't work. I checked it today 29-05-23 in job with Win 11 and the result was NT 10)