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 |1 Answer
Reset to default 2Replace
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
版权声明:本文标题:Why does an "Automation" Error occur when I run this VBA Excel script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744598679a2614935.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wmic /namespace:\\root\wmi path MSAcpi_ThermalZoneTemperature get CurrentTemperature
in a command prompt as administrator? – Storax Commented Mar 16 at 15:30