How To Get External IP Address via VB/VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TIves
    New Member
    • May 2012
    • 2

    How To Get External IP Address via VB/VBA

    This uses HTML scraping of this site:
    http://whatismyipaddre ss.com/

    Code can be changed to work with other sites if you're comfortable with String manipulation

    Paste this into a Module and run.

    Cheers!

    Trip

    Code:
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Dim tempString As String
    
    Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
        
    End Function
    
    Sub WhatsMyIP()
        
        Dim StartingPoint As Long
        Dim EndPoint As Long
        Dim i As Integer
        Dim IPAddress As String
    
        DownloadFile "http://whatismyipaddress.com/", "c:\tempfile.txt"
        
        Open "c:\tempfile.txt" For Random As #1 Len = 30000
        Get #1, 1, tempString
        Close #1
        StartingPoint = InStr(1, tempString, """LOOKUPADDRESS"" value=""", vbTextCompare)
        StartingPoint = StartingPoint + Len("""LOOKUPADDRESS"" value=""")
        EndPoint = InStr(1, Mid(tempString, StartingPoint, 25), """ max", vbTextCompare)
        IPAddress = Mid(tempString, StartingPoint, EndPoint - 1)
        MsgBox "Your External IP Address is: " & IPAddress
        
    End Sub
Working...