Here's an updated version that includes Windows Vista, 7 and 8:
Option Explicit
Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFOEX) As Integer
Public 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
Public Const VER_PLATFORM_WIN32s = 0
' Win32s on Windows 3.1
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
' Windows 95, Windows 98, or Windows Me
Public Const VER_PLATFORM_WIN32_NT = 2
''Windows NT, Windows 2000, Windows XP, Windows Vista/7/8, or Windows Server 2003/2008 family.
' Major Minor
' OS Platform Version Version Build
' Windows 95 1 4 0
' Windows 98 1 4 10 1998
' Windows 98SE 1 4 10 2222
' Windows Me 1 4 90 3000
' NT 3.51 2 3 51
' NT 2 4 0 1381
' 2000 2 5 0
' XP 2 5 1 2600
' Server 2003 2 5 2
Public Function GetVersion() As String
Const Failed As String = "Failed"
Dim OSInfo As OSVERSIONINFOEX
Dim retvalue As Integer
OSInfo.dwOSVersionInfoSize = Len(OSInfo)
OSInfo.szCSDVersion = Space$(128)
retvalue = GetVersionExA(OSInfo)
With OSInfo
Select Case .dwPlatformId
Case VER_PLATFORM_WIN32s ' Win32s on Windows 3.1
GetVersion = "Windows 3.1"
Case VER_PLATFORM_WIN32_WINDOWS ' Windows 95, Windows 98,
Select Case .dwMinorVersion ' or Windows Me
Case 0
GetVersion = "Windows 95"
Case 10
If (OSInfo.dwBuildNumber And &HFFFF&) = 2222 Then
GetVersion = "Windows 98SE"
Else
GetVersion = "Windows 98"
End If
Case 90
GetVersion = "Windows Me"
Case Else
GetVersion = Failed
End Select
Case VER_PLATFORM_WIN32_NT 'Windows NT, Windows 2000, Windows XP, Windows Vista/7/8,
Select Case .dwMajorVersion 'or Windows Server 2003/2008 family.
Case 3
GetVersion = "Windows NT 3.51"
Case 4
GetVersion = "Windows NT 4.0"
Case 5
Select Case .dwMinorVersion
Case 0
GetVersion = "Windows 2000"
Case 1
GetVersion = "Windows XP"
Case 2
GetVersion = "Windows Server 2003"
Case Else
GetVersion = Failed
End Select
Case 6
Select Case .dwMinorVersion
Case 0
If OSInfo.wProductType Then '(1 vs 0 is True vs False in VB/VBA too)
GetVersion = "Windows Vista"
Else
GetVersion = "Windows Server 2008"
End If
Case 1
If OSInfo.wProductType Then ' "
GetVersion = "Windows 7"
Else
GetVersion = "Windows Server 2008 R2"
End If
Case 2
GetVersion = "Windows 8"
Case Else
GetVersion = Failed
End Select
Case Else
GetVersion = Failed
End Select
Case Else
GetVersion = Failed
End Select
End With
End Function
And another very useful function:
Function IsGEWindowsVersion(CheckVersion As String) As Variant
'
'Returns True if the Windows version under which this code is running is >= the specified check-version. The
'currently supported check-versions are:
'
' "XP"
' "Vista"
' "7"
' "8"
'
'Note that "Windows Server 2003" is considered to be >= "XP", "Windows Server 2008" is considered to
'be >= "Vista", and "Windows Server 2008 R2" is considered to be >= "7".
'
'To add additional check-versions, refer to the GetVersion function's code.
'
'*************************************************************************************************************
Dim OSInfo As OSVERSIONINFOEX
Dim retvalue As Integer
OSInfo.dwOSVersionInfoSize = Len(OSInfo)
OSInfo.szCSDVersion = Space$(128)
retvalue = GetVersionExA(OSInfo)
'If retvalue = 1 Then 'GetVersionExA fails only if you mess up its passed argument
With OSInfo
If .dwPlatformId >= VER_PLATFORM_WIN32_NT Then
Select Case CheckVersion
Case "XP"
IsGEWindowsVersion = Not (.dwMajorVersion < 5 Or .dwMajorVersion = 5 And .dwMinorVersion < 1)
Case "Vista"
IsGEWindowsVersion = .dwMajorVersion > 6 Or .dwMajorVersion = 6 And .dwMinorVersion >= 0
Case "7"
IsGEWindowsVersion = .dwMajorVersion > 6 Or .dwMajorVersion = 6 And .dwMinorVersion >= 1
Case "8"
IsGEWindowsVersion = .dwMajorVersion > 6 Or .dwMajorVersion = 6 And .dwMinorVersion >= 2
End Select
End If
End With
'Else
' IsGEWindowsVersion = CVErr(2001) 'NOTE: To handle user-defined error, assign to a Variant and
' 'use IsError to check results.
'End If
End Function