Hide Command Shell window

C

Corey

Hi Folks,

I'm trying to use the following VBA code for verifying a list of server's DNS
record. Although the results are correct, the command shell window always
appears. Is any way to completely hide it? TIA.

Corey

Code:

Function MyNslookupExe(ByRef fstrFQDN As String, ByRef fstrRealIP As String,
fstrCmd As String)

' The function is to receive nslookup result.

Dim WSH, wExec
Dim Result As String
Dim tmp

fstrFQDN = ""
fstrRealIP = ""

Set WSH = CreateObject("WScript.Shell")
Set wExec = WSH.Exec(fstrCmd)

Result = wExec.StdOut.ReadAll

tmp = Split(Result, vbCrLf)

If tmp(3) <> "" And Left(tmp(3), 4) = strName Then

fstrFQDN = LCase(Trim(Right(tmp(3), Len(tmp(3)) - Len(strName) - 1)))
fstrRealIP = LCase(Trim(Right(tmp(4), Len(tmp(4)) - Len(strAddress)
- 1)))

End If

Set wExec = Nothing
Set WSH = Nothing

End Function
 
P

PeeDi

Maybe this code can help.........

Private Sub Workbook_Open()
Dim WSH
Set WSH = CreateObject("WScript.Shell")

WSH.Run ("cmd /K c:\test.wsf & exit")
'If I am not using "& exit" in the above command
'then the Command Shell window stays as it is and
'needs to be closed manually.
'In your case it also depends on variable "fstrCmd"
'Set wExec = WSH.Exec(fstrCmd)

Set WSH = Nothing

'You may also try this, you may use other constants listed below.
x = Shell("cmd.exe /c C:\test.wsf", vbMinimizedNoFocus)
End Sub

Constant Value Description
vbHide 0 Window is hidden and focus is passed
to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its
original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size
and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently
active window remains active.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top