admin管理员组

文章数量:1389356

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!

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!

Share asked Mar 16 at 13:52 dcsterdcster 192 bronze badges 2
  • Are you able to run wmic /namespace:\\root\wmi path MSAcpi_ThermalZoneTemperature get CurrentTemperature in a command prompt as administrator? – Storax Commented Mar 16 at 15:30
  • 1 To help others with a similar question, if you are entirely happy with an answer, please accept it by clicking on the grey check (tick) mark next to it. – JohnM Commented Mar 17 at 7:06
Add a comment  | 

1 Answer 1

Reset to default 2

Replace

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

with

Set objWMIService = GetObject("winmgmts:\\.\root\wmi")

The new code needs to be run as an administrator ... for example, if using Excel, close any existing Excel instances then right-click Excel in your Start menu, select 'Run as administrator' to start Excel, and then run your code.

本文标签: Why does an quotAutomationquot Error occur when I run this VBA Excel scriptStack Overflow