본문으로 바로가기
[엑셀 VBA] Snippet - Public IP, Local IP, Mac Address 구하기(퍼온글) - 바보처럼코딩하기

[엑셀 VBA] Snippet - Public IP, Local IP, Mac Address 구하기(퍼온글)

반응형

https://www.oppadu.com/정보공유/?mod=document&uid=23999 

 

[엑셀 VBA] Snippet - Public IP, Local IP, Mac Address 구하기

엑셀로 웹을 제어하려 하다 보니, 별의별거를 다 VBA로 구현하게 되네요.... ㅎㅎㅎ 가끔 쓸모가 좀 있습니다.   Option Explicit Function GetMyPublicIP() As String Dim HttpRequest As Object On Error Resume Next 'Create the

www.oppadu.com

Option Explicit
 
Function GetMyPublicIP() As String
 
    Dim HttpRequest As Object
 
    On Error Resume Next
    'Create the XMLHttpRequest object.
    Set HttpRequest = CreateObject("MSXML2.XMLHTTP")
 
    'Check if the object was created.
    If Err.Number <> 0 Then
        'Return error message.
        GetMyPublicIP = "Could not create the XMLHttpRequest object!"
        'Release the object and exit.
        Set HttpRequest = Nothing
        Exit Function
    End If
    On Error GoTo 0
 
    'Create the request - no special parameters required.
    HttpRequest.Open "GET", "http://myip.dnsomatic.com", False
 
    'Send the request to the site.
    HttpRequest.Send
 
    'Return the result of the request (the IP string).
    GetMyPublicIP = HttpRequest.ResponseText
 
End Function
 
Function GetMyLocalIP() As String
 
    'Declaring the necessary variables.
    Dim strComputer     As String
    Dim objWMIService   As Object
    Dim colItems        As Object
    Dim objItem         As Object
    Dim myIPAddress     As String
 
    'Set the computer.
    strComputer = "."
 
    'The root\cimv2 namespace is used to access the Win32_NetworkAdapterConfiguration class.
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
    'A select query is used to get a collection of IP addresses from the network adapters that have the property IPEnabled equal to true.
    Set colItems = objWMIService.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
    'Loop through all the objects of the collection and return the first non-empty IP.
    For Each objItem In colItems
        If Not IsNull(objItem.IPAddress) Then myIPAddress = Trim(objItem.IPAddress(0))
        Exit For
    Next
 
    'Return the IP string.
    GetMyLocalIP = myIPAddress
 
End Function
 
Function GetMyMACAddress() As String
 
    'Declaring the necessary variables.
    Dim strComputer     As String
    Dim objWMIService   As Object
    Dim colItems        As Object
    Dim objItem         As Object
    Dim myMACAddress    As String
 
    'Set the computer.
    strComputer = "."
 
    'The root\cimv2 namespace is used to access the Win32_NetworkAdapterConfiguration class.
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
    'A select query is used to get a collection of network adapters that have the property IPEnabled equal to true.
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
    'Loop through all the collection of adapters and return the MAC address of the first adapter that has a non-empty IP.
    For Each objItem In colItems
        If Not IsNull(objItem.IPAddress) Then myMACAddress = objItem.MACAddress
        Exit For
    Next
 
    'Return the IP string.
    GetMyMACAddress = myMACAddress
 
End Function
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유