View Full Version : [SLEEPER:] Why does an "Automation" Error occur when I run this VBA Excel script?
alfredoadams
03-30-2025, 09:06 PM
Hello,
I want to check the temperature of my CPU. I found the following code, but it doesn't seem to work, generating an Automation Error when it runs:
Sub GetCPU_Temperature()
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Dim temperature As Double
' Connect to WMI
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
' Query the temperature sensors
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSAcpi_ThermalZoneTemperature")
' Loop through the sensors and get the temperature
For Each objItem In colItems
temperature = objItem.CurrentTemperature
temperature = (temperature / 10) - 273.15 ' Convert from Kelvin to Celsius
MsgBox "CPU Temperature: " & temperature & " °C"
Next
End Sub
When I run the above code, I get the following error:
"Run-time error: '-2147217392 (80041010)': Automation Error"
I have the Microsoft WMI Scripting v.1.2 Library checked in my Reference Library.
Any help is appreciated!
Aussiebear
03-31-2025, 03:33 AM
Just guessing here, so maybe this might get you in the right direction.
Sub GetThermalInfo()
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Dim temperature As Double
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSAcpi_ThermalZoneTemperature")
For Each objItem In colItems
' Check this property
Debug.Print "InstanceName: " & objItem.InstanceName
' Check this property
Debug.Print "Caption: " & objItem.Caption
temperature = objItem.CurrentTemperature
temperature = (temperature / 10) - 273.15
Debug.Print "Temperature: " & temperature & " °C"
' You could add a condition here to only show a MsgBox if the
' InstanceName or Caption contains "CPU" (this is not foolproof)
If InStr(1, objItem.InstanceName, "CPU", vbTextCompare) > 0 Or _
InStr(1, objItem.Caption, "CPU", vbTextCompare) > 0 Then
MsgBox "Potential CPU Temperature: " & temperature & " °C"
End If
Next
End Sub
Aflatoon
03-31-2025, 05:15 AM
It's interesting that this post is word for word the same as the one asked (and answered) on SO here (https://stackoverflow.com/questions/79512646/why-does-an-automation-error-occur-when-i-run-this-vba-excel-script) two weeks ago...
georgiboy
03-31-2025, 06:05 AM
That is odd, I can't find any evidence of spam in the post which is usually why something like this would be done. The only strange thing I have noticed is that the user has gone to the effort to put a white full stop (period) in their signature...
alfredoadams
03-31-2025, 11:56 PM
Just guessing here, so maybe this might get you in the right direction.
Sub GetThermalInfo()
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Dim temperature As Double
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSAcpi_ThermalZoneTemperature")
For Each objItem In colItems
' Check this property
Debug.Print "InstanceName: " & objItem.InstanceName
' Check this property
Debug.Print "Caption: " & objItem.Caption
temperature = objItem.CurrentTemperature
temperature = (temperature / 10) - 273.15
Debug.Print "Temperature: " & temperature & " °C"
' You could add a condition here to only show a MsgBox if the ' InstanceName or Caption contains "CPU" (this is not foolproof)
If InStr(1, objItem.InstanceName, "CPU", vbTextCompare) > 0 Or _
InStr(1, objItem.Caption, "CPU", vbTextCompare) > 0 Then
MsgBox "Potential CPU Temperature: " & temperature & " °C"
End If
Next
End Sub
Thanks for your answer. I got it
Aflatoon
04-01-2025, 01:17 AM
Even more interesting since (as per the link I posted) that isn't the right namespace for the class in question.
georgiboy
04-01-2025, 01:27 AM
Indeed, very strange. Also, the signature was changed since yesterday, today it appeared as right aligned... I have removed the signature as it was pointless tbh.
Aussiebear
04-01-2025, 02:36 AM
Thanks for your answer. I got it
Hmmm.. somehow I don't think you have got it. Remember the answer you got over at stackoverflow?
Set objWMIService = GetObject "winmgmts:\\.\root\wmi". where you said it works... My response didn't include that.
To be honest I'm inclined to believe you are simply wasting our time.
Aflatoon
04-01-2025, 04:31 AM
Remember the answer you got over at stackoverflow?
I don't think this is the same person. I think the SO one was a genuine question...
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.