Last Updated 2012/02/24
Programming Tips Office  索 引 
VBA で IP アドレス取得
2012/02/24

VBA で IP アドレスを取得する。


'*****************************************************************************
' IP アドレス取得
'   WMI を用いて IP アドレスを取得する。
'*****************************************************************************
Function GetIPAddress() As String

    Dim NetAdapters, objNic, strIPAddress
    Set NetAdapters = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") _
                           .ExecQuery("Select * from Win32_NetworkAdapterConfiguration " & _
                           "Where (IPEnabled = TRUE)")

    For Each objNic In NetAdapters 'ネットワークアダプターは、複数ある場合がある
        For Each strIPAddress In objNic.IPAddress 'IPは、複数割り当てられている場合がある
            GetIPAddress = strIPAddress
            Exit For        ' 1回のみ
        Next
        Exit For        ' 1回のみ
    Next

End Function


参照
前後のTips
VBA で IP アドレス取得

DSS ProgrammingTipsCGI Ver2.02