본문 바로가기
카테고리 없음

[vba] Exec () 사용시 명령 프롬프트 창 숨기기( Hide command prompt window when using Exec())

by IT HUB 2020. 9. 19.
728x90
반응형
 

Sub 파일검색()

 

Dim dirPath As String, temPath As String, T As String

 

dirPath = """D:\My Project\Excel\*.xls*"""

temPath = "D:\tem.txt"

 

CreateObject("wscript.shell").Run "cmd /c dir " & _

dirPath & " /s /b > " & temPath, 0, False

 

Do Until Dir(temPath) <> "": DoEvents: Loop

Do Until FileLen(temPath) > 0: DoEvents: Loop

 

With CreateObject("Scripting.FileSystemObject")

T = .OpenTextFile(temPath).ReadAll()

On Error Resume Next

Do

If Err Then Err.Clear

.DeleteFile temPath

Loop Until Err = 0

End With

 

MsgBox T

 

End Sub

다른 방법으로 아래처럼 사용이 가능하지만 위와 결과가 좀 다르게 나온다.

Dim T As String, RunCommand As String

RunCommand = "cmd /c dir " & """D:\My Project\Excel\*.xls*"""

T = CreateObject("Wscript.Shell").Exec( _

RunCommand _

).StdOut.ReadAll

이와 같은 방법을 사용하면 cmd 창이 보인다는 단점이 있습니다.

"ipconfig" 를 실행 해보겠습니다.

Debug.? CreateObject("wscript.shell") _

.Exec("ipconfig /all") _

.StdOut.ReadAll()


간단히 내 컴퓨터의 IP를 확인하고자 할경우 사용할 수 있겠네요

실제 IP 확인하는 함수는 아래와 같이 사용합니다.

Function GetIPAddress()

Const strComputer As String = "." ' Computer name. Dot means local computer

Dim objWMIService, IPConfigSet, IPConfig, IPAddress, i

Dim strIPAddress As String

 

' Connect to the WMI service

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 

' Get all TCP/IP-enabled network adapters

Set IPConfigSet = objWMIService.ExecQuery _

("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

 

' Get all IP addresses associated with these adapters

For Each IPConfig In IPConfigSet

IPAddress = IPConfig.IPAddress

If Not IsNull(IPAddress) Then

strIPAddress = strIPAddress & Join(IPAddress, ", ")

End If

Next

 

GetIPAddress = strIPAddress

End Function

 

반응형


댓글