VBA:命令提示符响应在VBA中不显示



我试图通过VBA找到我的可用物理内存。我的目标是在VBA中运行systeminfo |查找"可用物理内存"命令并查看结果。由于某些原因,它不能工作,但可以与我尝试过的其他命令一起工作。我最初的想法是,我有代码编写的方式,它不是等待命令提示符完成。有什么想法或帮助吗?谢谢你

Sub MemoryCheck()
Dim availableMem As String
availableMem = ExecShellCmd("systeminfo |find ""Available Physical Memory""")
Debug.Print "availableMem: "; availableMem
End Sub
Public Function ExecShellCmd(FuncExec As String) As String
Dim wsh As Object, wshOut As Object, sShellOut As String, sShellOutline As String

Set wsh = CreateObject("WScript.Shell")

Set wshOut = wsh.exec(FuncExec).stdout

While Not wshOut.AtEndOfStream
sShellOutline = wshOut.ReadLine
If sShellOutline <> "" Then
sShellOut = sShellOut & sShellOutline & vbCrLf
End If
Wend

ExecShellCmd = sShellOut
End Function

这对我有用:

Sub MemoryCheck()
Dim availableMem As String
availableMem = ExecShellCmd("systeminfo |find ""Available Physical Memory""")
Debug.Print "availableMem: "; availableMem
End Sub
Public Function ExecShellCmd(FuncExec As String) As String
ExecShellCmd = VBA.CreateObject("WScript.Shell") _
.exec("cmd.exe /c " & FuncExec).stdout.readall
End Function

最新更新