📊 VBA (Excel)

Export Physical Memory Modules to CSV via WMI (VBA)

Queries Win32_PhysicalMemory via WMI and writes every Physical Memory Modules row straight to a CSV file in Documents, using plain VBA file I/O.

By WindowsScripting.com · 1.4 KB
PhysicalMemoryWmiExport.bas
Attribute VB_Name = "PhysicalMemoryWmiExport"
Option Explicit

Public Sub PhysicalMemoryWmiExport()
    Dim objWMIService As Object
    Dim colItems As Object
    Dim objItem As Object
    Dim outPath As String
    Dim fileNum As Integer
    Dim propsArr() As String
    Dim c As Long
    Dim lineOut As String
    Dim val As String
    Dim rowCount As Long

    outPath = Environ("USERPROFILE") & "\Documents\PhysicalMemoryExport.csv"
    propsArr = Split("BankLabel, Capacity, Speed, Manufacturer", ", ")

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("SELECT BankLabel, Capacity, Speed, Manufacturer FROM Win32_PhysicalMemory")

    fileNum = FreeFile
    Open outPath For Output As #fileNum
    Print #fileNum, Join(propsArr, ",")

    rowCount = 0
    For Each objItem In colItems
        lineOut = ""
        For c = LBound(propsArr) To UBound(propsArr)
            val = ""
            On Error Resume Next
            val = CStr(objItem.Properties_(propsArr(c)).Value)
            On Error GoTo 0
            If c > LBound(propsArr) Then lineOut = lineOut & ","
            lineOut = lineOut & """" & Replace(val, """", """""") & """"
        Next c
        Print #fileNum, lineOut
        rowCount = rowCount + 1
    Next objItem

    Close #fileNum

    MsgBox rowCount & " Physical Memory Modules row(s) exported to " & outPath, vbInformation
End Sub