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?
Thoughts?
I found the following at this link...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
[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]
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
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