VB to find operating system

Scott R

Active Member
Joined
Feb 20, 2002
Messages
493
Office Version
  1. 365
Platform
  1. Windows
We're moving from Windows 7 Pro to Windows 10 Enterprise. I'd like code in my workbook to work under either environment, meaning I need to determine which environment a user is in and adapt my code accordingly.

Thoughts?
 
I am not sure how useful that is... I am using the 64-bit version of Window 8.1 but your code say I am using...

Windows (32-bit) NT 6.02
I found the following at this link...

https://www.devhut.net/2016/09/24/vba-determine-the-installed-os/

and it works very well whether called within other VB code or called within a formula on the worksheet...
Code:
[table="width: 500"]
[tr]
	[td]'---------------------------------------------------------------------------------------
' Procedure : getOperatingSystem
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return the active OS details
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sHost     : Name/IP Address of the PC to query assuming you have the rights to do so
'               optional, so by leaving it blank it will query the local computer
'
' Usage:
' ~~~~~~
' ? getOperatingSystem()  -> Microsoft Windows 7 Ultimate  6.1.7601 (64-bit)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-09-27              Initial Release
' 2         2016-09-24              Code Cleanup and standardization
' 3         2018-08-30              Added OS bitness to returned value
'                                   Updated Copyright
'---------------------------------------------------------------------------------------
Public Function getOperatingSystem(Optional sHost As String = ".") As String
    'Win32_OperatingSystem -> https://msdn.microsoft.com/en-us/library/aa394239%28v=vs.85%29.aspx
    On Error GoTo Error_Handler
    Dim oWMI                  As Object    'WMI object to query about the PC's OS
    Dim oOSs                  As Object    'Collection of OSs
    Dim oOS                   As Object    'Individual OS
 
    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
    Set oOSs = oWMI.ExecQuery("SELECT Caption, Version, OSArchitecture FROM Win32_OperatingSystem")
 
    For Each oOS In oOSs    'Enumerate each OS provided by WMI
        getOperatingSystem = getOperatingSystem & oOS.Caption & " " & oOS.Version & _
                             " (" & oOS.OSArchitecture & "), "
    Next
    getOperatingSystem = Left(getOperatingSystem, Len(getOperatingSystem) - 2)    'Remove the last ", "
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oOS Is Nothing Then Set oOS = Nothing
    If Not oOSs Is Nothing Then Set oOSs = Nothing
    If Not oWMI Is Nothing Then Set oWMI = Nothing
    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[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I am not sure how useful that is... I am using the 64-bit version of Window 8.1 but your code say I am using...

Windows (32-bit) NT 6.02


Rick !!! Don't chastise me ... I'm on meds and I've been drinking heavily. I simply can't take it ... :laugh:

Ok ... I borrowed your last code snippet and added a MsgBox to it ... (in an attempt to redeem myself) - Pretty cheezy ... huh ? :

Code:
 Option Explicit'---------------------------------------------------------------------------------------
' Procedure : getOperatingSystem
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return the active OS details
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sHost     : Name/IP Address of the PC to query assuming you have the rights to do so
'               optional, so by leaving it blank it will query the local computer
'
' Usage:
' ~~~~~~
' ? getOperatingSystem()  -> Microsoft Windows 7 Ultimate  6.1.7601 (64-bit)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-09-27              Initial Release
' 2         2016-09-24              Code Cleanup and standardization
' 3         2018-08-30              Added OS bitness to returned value
'                                   Updated Copyright
'---------------------------------------------------------------------------------------
Public Function getOperatingSystem(Optional sHost As String = ".") As String
    'Win32_OperatingSystem -> https://msdn.microsoft.com/en-us/library/aa394239%28v=vs.85%29.aspx
    On Error GoTo Error_Handler
    Dim oWMI                  As Object    'WMI object to query about the PC's OS
    Dim oOSs                  As Object    'Collection of OSs
    Dim oOS                   As Object    'Individual OS
 
    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
    Set oOSs = oWMI.ExecQuery("SELECT Caption, Version, OSArchitecture FROM Win32_OperatingSystem")
 
    For Each oOS In oOSs    'Enumerate each OS provided by WMI
        getOperatingSystem = getOperatingSystem & oOS.Caption & " " & oOS.Version & _
                             " (" & oOS.OSArchitecture & "), "
    Next
    getOperatingSystem = Left(getOperatingSystem, Len(getOperatingSystem) - 2)    'Remove the last ", "
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oOS Is Nothing Then Set oOS = Nothing
    If Not oOSs Is Nothing Then Set oOSs = Nothing
    If Not oWMI Is Nothing Then Set oWMI = Nothing
    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


Sub sysInfo()
    MsgBox getOperatingSystem()
End Sub
 
Upvote 0
.
Rick ... saw where you "one upped me" on another post. It's been a long day and I'm going to take my Social Security nap now.

Things will be better later today.

Bye ...
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top