定义以下过程即可获取本机ip地址
Sub GetMyIP()
Dim strComputer As String
Dim objWMI As Object
Dim colIP As Object
Dim IP As Object
Dim I As Integer
strComputer = "."
Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Set colIP = objWMI.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each IP In colIP
If Not IsNull(IP.IPAddress) Then
For I = LBound(IP.IPAddress) To UBound(IP.IPAddress)
MsgBox "IP 地址:" & IP.IPAddress(I) & Chr(10) & _
Next
End If
Next
End Sub
加入一个Winsock 控件
Winsock1.LocalIP 就是本机IP
Private Sub Command1_Click()
Set oAdapters = GetObject("winmgmts:").ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")
For Each oAdapter In oAdapters
For Each strAddress In oAdapter.IPAddress
Print strAddress
Next
Next
End Sub
在部件中加一个Microsoft Winsock Control 6.0控件
然后拉到窗体里
代码中用Winsock1.LocalIP就是本机IP地址比如
Private Sub Command1_Click()
Print Winsock1.LocalIP
End Sub
使用Winsock控件(工程-部件 Microsoft Winsock Control 6.0)
LocalIP属性 返回本地IP