VBA 获取本机IP

发布时间 2023-10-07 16:19:24作者: sonycat
Function GetIP() As String
    Dim OpSysSet As Object, Op As Object, Ip
    Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate}//localhost").ExecQuery("SELECT index, IPAddress FROM Win32_NetworkAdapterConfiguration")
    For Each Op In OpSysSet
        If Not IsNull(Op.IPAddress) Then
            For Each Ip In Op.IPAddress
                If Ip Like "*.*.*.*" Then
                    GetIP = Ip
                    Exit Function
                End If
            Next
        End If
    Next
End Function