Splitting an IP address

M

Mark

I am using Word 97.

I have used some code which one of the MVP's provided which picks up the IP
address of a computer.

I want now to split the address say from:

010.016.065.002

so that it only picks up the third section

..065.

if its 2 digits adds a leading zero

..65. to .065.

then so that it strips out the dots at either side.

Can anyone assist, please.
 
A

Adrian

'18 Nov 2004
Private Sub TestSomething()

Dim arrIP As Variant
Dim s3rdSection As String


arrIP = Split("010.016.65.002", ".") 'Creates zero-based array split
at period

s3rdSection = Format(arrIP(2), "000") 'Formats 3rd element (starting
zero) to have leading zeros


End Sub
 
J

Jonathan West

Hi Mark

This should do the trick

Function ThirdOctet(strIP As String) As String
Dim vSplit As Variant
vSplit = Split(strIP, ".")
ThirdOctet = Format(vSplit(2), "000")
End Function
 
M

Mark

Brilliant.

Many thanks

Adrian said:
'18 Nov 2004
Private Sub TestSomething()

Dim arrIP As Variant
Dim s3rdSection As String


arrIP = Split("010.016.65.002", ".") 'Creates zero-based array split
at period

s3rdSection = Format(arrIP(2), "000") 'Formats 3rd element (starting
zero) to have leading zeros


End Sub
 
M

Mark

Adrain,

It doesn't like the split command!

It debugs saying sub or function not defined.

Any solutions?

Mark
 
M

Mark

Many thanks Jonathan

Mark

Jonathan West said:
Hi Mark

This should do the trick

Function ThirdOctet(strIP As String) As String
Dim vSplit As Variant
vSplit = Split(strIP, ".")
ThirdOctet = Format(vSplit(2), "000")
End Function

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 
A

Adrian

So sorry, Mark! You did say you are using Word 97, which does not have the
Split() function.
 
A

Adrian

OK, Mark... step through this code!

'18 Nov 2004
Private Sub TestSomething()

Dim sStrToSplit As String, sChr As String
Dim sReqdSection As String
Dim i As Integer, nDotCount As Integer


sStrToSplit = "010.016.65.002"
nDotCount = 1

For i = 1 To Len(sStrToSplit)
sChr = Mid$(sStrToSplit, i, 1)
If sChr = "." Then nDotCount = nDotCount + 1

'Set nDotCout to return required section
If nDotCount = 4 Then sReqdSection = sReqdSection & sChr
Next

If Left$(sReqdSection, 1) = "." Then sReqdSection = Mid$(sReqdSection, 2)

sReqdSection = Format(sReqdSection, "000")

End Sub
 
H

Helmut Weber

Hi everybody,
Hi Mark
you must be a very polite person,
as Jonathan's example is using split, too.
---
Sub test666()
Dim s As String
Dim p As Integer ' a position
s = "010.016.065.002"
p = InStr(s, ".")
s = Right(s, Len(s) - p)
p = InStr(s, ".")
s = Right(s, Len(s) - p)
p = InStr(s, ".")
s = Left(s, p - 1)
s = Format(s, "000")
MsgBox s
End Sub
Of course, this could be converted into a function.
You could even model your own split function with Word 97.
But if it is only an IP-adress...
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top