VBA Function To Return OS Version

gduron

Board Regular
Joined
Mar 27, 2006
Messages
94
Hello,

I have a Function that returns the Operating system that is running on a computer. This code works well for me and numerous other people, but there is 1 person that when they run this code they get an error.

The error that this individual encounters is on the line:

"Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & localHost & "\root\cimv2")"

I think it might have something to do with my variable "localHost", but I'm not totally sure. I'm hoping somebody else has encountered this problem before.

Code:
Public Function getOperatingSystem()

Dim localHost As String, objWMIService As Variant, colOperatingSystems As Variant
Dim objOperatingSystem As Variant

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
    
End Function

Thanks in advance.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Which missing references are you referring to? The function is in an XLA file and it runs fine on multiple machines. I'm not sure what reference would be missing on the machine it does not work on.

Thanks
 
Upvote 0
Sorry. I don't meant to be drawing a complete blank here, but I need to understand where I am supposed to add the reference.

Where is the below menu that you are referring to?
Tools > References

If I put that same function in a vbs file and run it on my machine it works. If I run it on the machine that is throwing the error it does not. Is there a references library located outside of the Microsoft VBA editor?
 
Upvote 0
This posting refers only to the reference library from a VBA perspective. Is there a separate library that would need to be updated for VBS?
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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