投稿‎ > ‎

Run DOS Command in VBA

posted Mar 1, 2016, 8:50 PM by Zhang Wenxu   [ updated Jul 12, 2016, 6:33 PM ]
Public Const TemporaryFolder = 2
Public Function runCmd(strCmd As String) As String
    Dim fso As New FileSystemObject
    Dim tempFile As String
    Dim wsh As New WshShell
    Dim waitOnReturn As Boolean
    Dim windowStyle As Integer

    tempFile = fso.GetSpecialFolder(TemporaryFolder) & "\" & Replace(fso.GetTempName(),".tmp", ".txt")
    strCmd = "cmd /c " & strCmd & " > " & tempFile
    waitOnReturn = True
    windowStyle = 0
    wsh.Run strCmd, windowStyle, waitOnReturn
    On Error Resume Next
    runCmd = fso.OpenTextFile(tempFile, ForReading,  False).ReadAll
End Function

参照設定:
Windows Script Host Object Model
Microsoft Scripting Runtime

Comments