You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Excel中判断用户电脑运行的是Windows 10还是Windows 11

准确判断Windows 10/11版本的VBA方案

之前通过注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName读取系统名称的方法不可靠,原因是微软从Windows 11开始,该键值可能保留旧标识,且部分系统会因兼容性设置、版本更新出现偏差。以下是几种可靠的判断方案:

方法1:读取注册表内部构建版本号

Windows 10的内部版本号范围为10240~21999,Windows 11的内部版本号从22000开始(正式版初始为22000,后续更新会更高)。通过读取CurrentBuildNumber项判断:

Dim oWsh As Object
Set oWsh = CreateObject("WScript.Shell")
Dim buildNum As String
buildNum = oWsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber")

If CLng(buildNum) >= 22000 Then
    ' 执行Windows 11适配逻辑
    MsgBox "Windows 11"
Else
    ' 执行Windows 10适配逻辑
    MsgBox "Windows 10"
End If
Set oWsh = Nothing

注意:CurrentBuildNumber是字符串类型,必须转换为数值再比较,避免字符串排序导致的错误(比如"9999"和"10000"的字符串比较结果不符合预期)。

方法2:调用Windows API(兼容多系统)

通过GetVersionEx API获取系统版本信息,需适配32位/64位Excel:

#If VBA7 Then
    Declare PtrSafe Function GetVersionEx Lib "kernel32" Alias "GetVersionExW" (lpVersionInformation As OSVERSIONINFOEX) As Long
#Else
    Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExW" (lpVersionInformation As OSVERSIONINFOEX) As Long
#End If

Type OSVERSIONINFOEX
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
    wServicePackMajor As Integer
    wServicePackMinor As Integer
    wSuiteMask As Integer
    wProductType As Byte
    wReserved As Byte
End Type

Sub CheckWindowsVersion()
    Dim osInfo As OSVERSIONINFOEX
    osInfo.dwOSVersionInfoSize = Len(osInfo)
    
    If GetVersionEx(osInfo) Then
        If osInfo.dwMajorVersion = 10 Then
            If osInfo.dwBuildNumber >= 22000 Then
                MsgBox "Windows 11"
            Else
                MsgBox "Windows 10"
            End If
        End If
    End If
End Sub

说明:Windows 10和11的主版本号均为10,核心区别在于内部构建版本号,22000及以上为Windows 11。

方法3:WMI查询系统名称

通过WMI的Win32_OperatingSystem类直接获取系统名称,适合英文系统环境:

Dim objWMIService As Object
Dim colOperatingSystems As Object
Dim objOS As Object

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

For Each objOS In colOperatingSystems
    If InStr(objOS.Caption, "Windows 11") > 0 Then
        MsgBox "Windows 11"
    ElseIf InStr(objOS.Caption, "Windows 10") > 0 Then
        MsgBox "Windows 10"
    End If
Next

Set objWMIService = Nothing
Set colOperatingSystems = Nothing
Set objOS = Nothing

注意:非英文系统中Caption属性会显示对应语言的系统名称,此时建议优先用版本号判断而非文本匹配。

内容的提问来源于stack exchange,提问作者Balloon Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 09:45:09