代码之家  ›  专栏  ›  技术社区  ›  Harun24hr

Excel VBA:仅从对象获取特定属性(Properties.Name)需要帮助吗

  •  1
  • Harun24hr  · 技术社区  · 6 年前

    我正在尝试获取处理器信息,特别是像 Intel(R)Core(TM)i5-7200U CPU@2.50GHz .

    在网上搜索时,我发现了一个函数,它使用 for-each 循环。

    name property 没有for each循环或其他方法,因此只能获取处理器名称。我将使用此代码收集 硬盘、RAM、处理器 信息但不是所有的信息只有名称、大小等。

    Sample File Download Link

    Public oWMISrvEx As Object 'SWbemServicesEx
    Public oWMIObjSet As Object 'SWbemServicesObjectSet
    Public oWMIObjEx As Object 'SWbemObjectEx
    Public oWMIProp As Object 'SWbemProperty
    Public sWQL As String 'WQL Statement
    Public n
    
    
    Sub ProcessorWMI()
    Dim sht As Worksheet
    
    Set sht = ThisWorkbook.Sheets("Processor")
    
    sWQL = "Select * From Win32_Processor"
    Set oWMISrvEx = GetObject("winmgmts:root/CIMV2")
    Set oWMIObjSet = oWMISrvEx.ExecQuery(sWQL)
    intRow = 2
    strRow = Str(intRow)
    
    sht.Range("A1").Value = "Name"
    sht.Cells(1, 1).Font.Bold = True
    
    sht.Range("B1").Value = "Value"
    sht.Cells(1, 2).Font.Bold = True
    
    For Each oWMIObjEx In oWMIObjSet
        For Each oWMIProp In oWMIObjEx.Properties_
            If Not IsNull(oWMIProp.Value) Then
    
                If IsArray(oWMIProp.Value) Then
                    For n = LBound(oWMIProp.Value) To UBound(oWMIProp.Value)
                        Debug.Print oWMIProp.Name & "(" & n & ")", oWMIProp.Value(n)
                          sht.Range("A" & Trim(strRow)).Value = oWMIProp.Name
                            sht.Range("B" & Trim(strRow)).Value = oWMIProp.Value(n)
                            sht.Range("B" & Trim(strRow)).HorizontalAlignment = xlLeft
                           intRow = intRow + 1
                        strRow = Str(intRow)
                    Next
    
                Else
                    sht.Range("A" & Trim(strRow)).Value = oWMIProp.Name
                    sht.Range("B" & Trim(strRow)).Value = oWMIProp.Value
                            sht.Range("B" & Trim(strRow)).HorizontalAlignment = xlLeft
                    intRow = intRow + 1
                    strRow = Str(intRow)
                End If
    
            End If
        Next
    Next
    End Sub
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Siddharth Rout    6 年前

    获取处理器名称的最简单方法

    Sub ProcessorName()
        Dim objPross As Object, cpu As Object
    
        Set objPross = GetObject("WinMgmts:").instancesof("Win32_Processor")
    
        For Each cpu In objPross
            Debug.Print cpu.Name
        Next
    End Sub
    

    当我运行代码时 Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz .

    这将达到我的目的。不管怎样,有什么财产清单吗

    你可以直接从注册表中读取名字吗?

    Sub ProcessorName()
        Dim objWsScript As Object
    
        Set objWsScript = CreateObject("WScript.Shell")
    
        Debug.Print objWsScript.RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0\ProcessorNameString")
    End Sub
    
    推荐文章