Need help. with locating my IP ADD. Current code giving error!

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
I have search here and there...am trying to let vba give me my IP address though i know where it is located. Please tell me when this is going wrong
getting error message "File not found"!:biggrin:
Code:
Option Explicit
 Sub IPtest()
    Dim wsh As Object
    Dim RegEx As Object, RegM As Object
    Dim FSO As Object, fil As Object
    Dim ts As Object, txtAll As String, TempFil As String
    Set wsh = CreateObject("WScript.Shell")
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set RegEx = CreateObject("vbscript.regexp")
    TempFil = "C:\myip.txt"
     ' Save ipconfig info to temporary file
    wsh.Run "%comspec% /c ipconfig > " & TempFil, 0, True
    With RegEx
        .Pattern = "(\d{1,3}\.){3}\d{1,3}"
        .Global = False
    End With
    Set fil = FSO.GetFile(TempFil)
     ' Access temporary file
    Set ts = fil.OpenAsTextStream(1)
    txtAll = ts.ReadAll
    Set RegM = RegEx.Execute(txtAll)
     ' Return IP address to Activesheet cell A1 by parsing text
    ActiveSheet.Range("A1").Value = RegM(0)
    ActiveSheet.Range("A1").EntireColumn.AutoFit
    ts.Close
     ' Remove temp file
    Kill TempFil
     
    Set ts = Nothing
    Set wsh = Nothing
    Set fil = Nothing
    Set FSO = Nothing
    Set RegM = Nothing
    Set RegEx = Nothing
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Pedie

Unless you have administrative rights, to save in that location, the code will fail on the line below, try changing this line

Code:
TempFil = "C:\myip.txt"

To something like this, alter for your location of course.

Code:
TempFil = "C:\Users\Desk\Documents\myip.txt"
 
Upvote 0
It gave me a number which does not match the IP address from the website....is there different IP for internet and local IP something like that???
or just one IP...otherwise this code is giving me a wrong IP add:)

Ped;)
 
Upvote 0
Brian, thank alot!!!!
It is giving correct IP add...:biggrin:

Thanks alot!

to validate it i type window > run > type 'cmd' hit enter> type ipconfig hit enter...worked..!!!
 
Upvote 0
yes..it worked. Thanks alot!!!:biggrin:

Even you can try it...in cells(1) it gives us the Users IP address :)
 
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,925
Members
449,274
Latest member
mrcsbenson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top