如何确定用户的个人电脑正在运行Windows 10还是Windows 11以供Excel函数使用?

huangapple go评论63阅读模式
英文:

How can I determine if a user's PC is running Windows 10 or Windows 11 for Excel functions?

问题

我有一个由几个其他用户使用的Excel工作簿。我发现一些功能在不同的Windows操作系统上运行方式不同。我的工作簿需要根据不同的Windows系统做出反应。

过去,

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

会告诉我版本,即"Windows 10企业版"。实际上有点奇怪,因为当我查看注册表时,它显示"Windows 10专业版"……"企业版"从哪里来?

无论如何,当我在Win 11上运行相同的命令时,它仍然显示"Windows 10专业版"。

我如何知道其他人的PC上的操作系统是Win 10还是Win 11?

如详细信息中所述。

英文:

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.

答案1

得分: 3

你可以通过使用WMI了解很多信息。它相当神秘,而在VBA中执行它也不会变得更简单。

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

如果你想了解为什么这段代码有效,可以学习一下WMI。你也可以在Powershell中使用类似以下命令来执行类似的操作:

Get-ComputerInfo | select OsName

但然后你需要调用它并获取答案。

英文:

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.

答案2

得分: 1

以下是来自链接的代码部分的翻译:

作者发布的代码

' 用于确定 Windows 版本号的函数

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 = "版本是 " & osinfo.dwMajorVersion & "." & osinfo.dwMinorVersion

End Function

回复中的代码

'---------------------------------------------------------------------------------------
' 过程:getOperatingSystem
' 作者:Daniel Pineault, CARDA Consultants Inc.
' 网站:http://www.cardaconsultants.com
' 目的:返回活动操作系统的详细信息
' 版权:只要版权声明保持不变(包括作者、网站和
'
' 修订历史:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-Sep-27                 初始发布
'---------------------------------------------------------------------------------------
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 = "." ' 技术上可以针对允许的情况远程运行
    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 "发生了以下错误。" & vbCrLf & vbCrLf & _
           "错误编号:" & Err.Number & vbCrLf & _
           "错误来源:getOperatingSystem" & vbCrLf & _
           "错误描述:" & Err.Description, _
           vbCritical, "发生错误!"
    Resume Error_Handler_Exit
End Function

希望这对你有帮助。

英文:

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

答案3

得分: 0

Debug.Print Application.OperatingSystem

(但正如@Sam所说,它不起作用。我今天在Win 11的工作中检查了一下,结果是NT 10)

英文:

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)

huangapple
  • 本文由 发表于 2023年5月29日 00:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定