VBA Get My Public IP Address

What is my Public IP Address?

There are no direct methods to get public ip address in VBA.

This has to be done by using a web service using below steps:

  1. Open IE Explorer from VBA Code.
  2. Navigate to this page -> What is my Ip?
  3. Wait for page to load
  4. Get IP address displayed in page.

So, what is does is actually extract IP address displayed external web site.

VBA Get My IP

Here is the code to display the ip address.

Public Sub Whatismyip()
    'Define Data type for Variable Fields
    Dim inetApp As Object
    Dim sURL As String
    
    'Set URL to What is my ip page
    sURL = "https://officetricks.com/what-is-my-ip-address/"
    
    'Create IE & Navigate
    Set inetApp = CreateObject("InternetExplorer.Application")
    inetApp.Visible = True
    inetApp.navigate sURL
    DoEvents
    Application.Wait DateAdd("s", 1, Now)
    Do While inetApp.busy
        Application.StatusBar = "Application Loading. Please Wait..."
        Application.Wait DateAdd("s", 1, Now)
        DoEvents
    Loop
    
    'Get the IP
    Set t0 = inetApp.document.getElementsByClassName("whatismyip")
    For Each t1 In t0
        Debug.Print t1.innertext
        'or
        'MsgBox t1.innertext
        Exit For
    Next    
End Sub

You have plenty of methods available to get local ip address like cmd->ipconfig or windows management etc.,