Get your IP address

Asrampd

Board Regular
Joined
Feb 26, 2012
Messages
247
Hi, Taken from the net - I think this was written for an earlier version of Excel than mine (2007). Can anyone tel me what to alter, it does not like this line...

ActiveSheet.Range("A1").Value = RegM(0)



Option Explicit

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


Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi Asrampd

I am using 2007 and stepped through this code and it worked fine

Kevin
 
Upvote 0
I tried it again on an XP machine and it stepped thru ok.

I also tried your alternative, which looks better, but dont see any IP addres appear in the Spread sheet cells. Do I have to call this in some way. (Total beginner).
 
Upvote 0
or this oneliner
Code:
Sub IPtest_snb()
 MsgBox Join(Filter(Split(CreateObject("WScript.Shell").exec("cmd /c ipconfig").stdout.readall, vbCrLf), ". :"), vbLf)
End Sub
 
Last edited:
Upvote 0
Hi SNB

Thanks for the reply - could you explain what it does and how i soulld use /call it ?

Ta.
 
Upvote 0
Where should I put what you suggested - at the bottom of the rest of the code should I add a Module . Do I need to put something in a cell or does running it put data into the cell ?

Cheers
 
Upvote 0

Forum statistics

Threads
1,221,564
Messages
6,160,513
Members
451,655
Latest member
rugubara

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