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

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

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

Sub파일검색()

DimdirPathAsString, temPathAsString, TAsString

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 ResumeNext

Do

IfErrThenErr.Clear

.DeleteFile temPath

Loop Until Err=0

EndWith

MsgBox T

EndSub

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

DimTAsString, RunCommandAsString

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 strComputerAsString="."' Computer name. Dot means local computer

DimobjWMIService, IPConfigSet, IPConfig, IPAddress, i

DimstrIPAddressAsString

' Connect to the WMI service

SetobjWMIService=GetObject("winmgmts:"_

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

' Get all TCP/IP-enabled network adapters

SetIPConfigSet=objWMIService.ExecQuery _

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

' Get all IP addresses associated with these adapters

ForEach IPConfig In IPConfigSet

IPAddress=IPConfig.IPAddress

IfNotIsNull(IPAddress)Then

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

EndIf

Next

GetIPAddress=strIPAddress

EndFunction

반응형


댓글