2017-09-22 65 views
0

我是vba編程的新手,我在一箇舊論壇中發現了這個功能。 我想添加一個選項,在最後關閉cmd窗口,用於學習目的。 這是cmd窗口打開的功能。運行命令結果保存爲字符串。如何關閉vba中已打開的cmd窗口?

Public Function ShellRun(sCmd As String) As String 

Dim oShell As Object 
Set oShell = CreateObject("WScript.Shell") 


Dim oExec As Object 
Dim oOutput As Object 
Set oExec = oShell.Exec(sCmd) 
Set oOutput = oExec.StdOut 

Dim s As String 
Dim sLine As String 
While Not oOutput.AtEndOfStream 
    sLine = oOutput.ReadLine 
    If sLine <> "" Then s = s & sLine & vbCrLf 
Wend 

ShellRun = s 

End Function 
+0

嘗試'oShell.Quit'。 – UGP

+0

Thx它幾秒鐘後才能正常工作 – Mocty

回答