Subscribe Now!
New videos every Weekend!
Like what I do? Donate
Did I help you? Did one of my tutorials save you sometime?
You can say thank you by buying me a cup of coffee, I go through a lot of it.
Help keep Greater Good resources free for everyone. Please donate today.
Download project or source code from below link(s)
Use the below code to extract/retrieve System or Network Ip addresses using VBA.
Paste the code in the module.
SysIPAddress function is a master function to connect WMI service and return IP address from Win32_NetworkAdapterConfiguration. Please comment below if you need any assistance.
Subroutine tst_SysIPAddress is used to test the main code. If you run this code it will print all your ip addresses in your immediate window. Have fun!
Paste the code in the module.
SysIPAddress function is a master function to connect WMI service and return IP address from Win32_NetworkAdapterConfiguration. Please comment below if you need any assistance.
Subroutine tst_SysIPAddress is used to test the main code. If you run this code it will print all your ip addresses in your immediate window. Have fun!
Option Explicit
Function SysIPAddress() As Variant
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Channel: Youtube.com/vbaa2z
'Download free codes from http://vbaa2z.blogspot.com
'Autor: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
Dim WMI_service As Variant
Dim myIP_Adptr As Variant
Dim IPConfiguration As Variant
Dim IPAddress As Variant
Dim XipAdd As String
Dim k() As Variant
Dim f As Byte
'setup WMI service
Set WMI_service = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
'setup TCP/IP-enabled network adapters
Set myIP_Adptr = WMI_service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
'loop and get all IP addresses associated with myIP_Adptr
For Each IPConfiguration In myIP_Adptr
IPAddress = IPConfiguration.IPAddress
If Not IsNull(IPAddress) Then
f = f + 1
ReDim Preserve k(1 To f)
k(f) = IPAddress
End If
Next
SysIPAddress = k()
'free memory;
Set WMI_service = Nothing
Set myIP_Adptr = Nothing
End Function
Sub tst_SysIPAddress()
Dim ArrMem() As Variant
Dim i As Byte
ArrMem = SysIPAddress()
For i = 0 To UBound(ArrMem)
Debug.Print ArrMem(1)(i)
Next
End Sub
thanks. this helps.
ReplyDelete