I have seen some VBA functions that return the IP address of the
PC (See sample below) but it only seems to work with Win XP.
Does anyone know how to achieve this with Windows 7?
' code sample ____________________________________________________
Dim IPAddress As String
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
On Error GoTo GetLocIP
Set wsh = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set RegEx = CreateObject("vbscript.regexp")
TempFile = "C:\myipaddress.txt"
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(TempFile)
Set ts = fil.OpenAsTextStream(1)
txtAll = ts.ReadAll
Set RegM = RegEx.Execute(txtAll)
GetLocIP = RegM(0)
ts.Close
Kill TempFil
Set ts = Nothing
Set wsh = Nothing
Set fil = Nothing
Set FSO = Nothing
Set RegM = Nothing
Set RegEx = Nothing
'end of example_________________________________
PC (See sample below) but it only seems to work with Win XP.
Does anyone know how to achieve this with Windows 7?
' code sample ____________________________________________________
Dim IPAddress As String
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
On Error GoTo GetLocIP
Set wsh = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set RegEx = CreateObject("vbscript.regexp")
TempFile = "C:\myipaddress.txt"
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(TempFile)
Set ts = fil.OpenAsTextStream(1)
txtAll = ts.ReadAll
Set RegM = RegEx.Execute(txtAll)
GetLocIP = RegM(0)
ts.Close
Kill TempFil
Set ts = Nothing
Set wsh = Nothing
Set fil = Nothing
Set FSO = Nothing
Set RegM = Nothing
Set RegEx = Nothing
'end of example_________________________________