Mocty
Mocty

Reputation: 35

How do I Close a already open cmd Window in vba?

I'm new to programming with vba and I found this funtion in an old forum. I would like to add the option that at the end the cmd window be closed, for learning purposes. This is the Function where the cmd window is open. Running the command the result is save as a string.

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

Upvotes: 0

Views: 2620

Answers (1)

kaza
kaza

Reputation: 2327

Set objShell = Nothing

See the link below at the example section. https://ss64.com/vb/shell.html

Upvotes: 1

Related Questions