Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IP Address (Word 97SR2)

    Anyone have a nice function that returns the PC's IP address?
    Looking for something succinct.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: IP Address (Word 97SR2)

    Here's a sample project that includes a class module I use.

    I hope this helps.

    Cheers,

    Kevin Bell
    Attached Files Attached Files

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Kevin,
    Thanks. Good of you to share your code. Isn't it interesting, though, that it takes 6
    API calls and all that memory moving stuff just to get the IP address? Oh well, it
    works and I'm tuckered out.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    By chance I wasa reading Tales from the Script - October 2002 at MS TechNet and derived the following code which worked in Windows XP using Office XP. Not an ideal solution (if it even works on your system) but a curiosity that does not use too many lines and no API calls. <pre>Dim IP_Array() As String, x As Long
    Set objShell = CreateObject("WScript.Shell")
    Set objWshScriptExec = objShell.Exec("ipconfig /all")
    Set objStdOut = objWshScriptExec.StdOut
    x = 0
    While Not objStdOut.AtEndOfStream
    ReDim IP_Array(x)
    strLine = objStdOut.ReadLine
    If InStr(strLine, "IP Address") Then
    IP_Array(x) = Mid(strLine, 45, 12)
    Debug.Print IP_Array(x)
    x = x + 1
    End If
    Wend</pre>

    Andrew

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    I was on my way to do something similar with either ipconfig or nbtstat
    or arp -a or route print or netstat -n or (gee wiz) when
    BigKev offered his class mod.

    On a Win9x PC, winipcfg /all may work instead of ipconfig /all.

    I like it. Very good use of the scripting shell.
    Hummm. Maybe I'll pull the class mod... No offense there
    Bigkev.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Option Explicit will force a few more declarations in the code. FYI.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  7. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Of course, and also amend

    IP_Array(x) = Mid(strLine, 45, 16)

    Andrew

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Yes, and I don't like the screen flash as the "dos" box opens and closes.

    Hummm. Back to the APIs in the class mod...

    VBA "bods" (as Beryl call us) are such a fickle lot.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Make that

    IP_Array(x) = Mid(strline, 38, 16)

    for it to work on my Win2k sp3 box.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    For what it's worth:

    (cleaned up a bit)

    Function IPAddr() As String
    Dim sIP As String
    Dim objShell As Object
    Dim objWshScriptExec As Object
    Dim objStdOut As Object
    Dim strline As String

    Set objShell = CreateObject("WScript.Shell")
    Set objWshScriptExec = objShell.Exec("ipconfig /all")
    Set objStdOut = objWshScriptExec.StdOut

    While Not objStdOut.AtEndOfStream
    strline = objStdOut.ReadLine
    If InStr(strline, "IP Address") Then
    sIP = Mid(strline, 38, 16)
    End If
    Wend
    IPAddr = Trim(sIP)
    End Function
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Kevin, just another little tweak to make it more generic :

    Function IPAddr() As String
    Dim sIP As String
    Dim objShell As Object
    Dim objWshScriptExec As Object
    Dim objStdOut As Object
    Dim strline As String
    Dim intIPStart As Integer, intIPLen As Integer

    Set objShell = CreateObject("WScript.Shell")
    Set objWshScriptExec = objShell.Exec("ipconfig /all")
    Set objStdOut = objWshScriptExec.StdOut

    While Not objStdOut.AtEndOfStream
    strline = objStdOut.ReadLine
    If InStr(strline, "IP Address") Then
    intIPStart = InStr(strline, ": ") + 2
    intIPLen = Len(strline) - intIPStart
    sIP = Mid(strline, intIPStart, intIPLen)
    End If
    Wend

    IPAddr = sIP

    End Function

    This worked on NT where sIP = Mid(strline, <font color=red>32</font color=red>, 16) would be required otherwise.

    I initially used an arrary because it is possible to have more that one IP addresss if more tham one network (including virtual) exists. If more then one IP address exists the above will only return the last one encountered by ipconfig, which may not be the correct one !

    Andrew C

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    I forgot about multi-homed PCs. Good work.

    Here's another method a friend showed me. It may not work unless the
    library is installed on your machine, but give it a go.

    Sub MaybeIPAddress()
    Dim ws As Object
    Set ws = CreateObject("MSWinsock.Winsock")
    Debug.Print ws.Localip
    End Sub
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: IP Address (Word 97SR2)

    > It may not work unless the library is installed on your machine

    It just displays the error message "Run time error '429' ActiveX component can't create object" on my W98SE PC.

    Pity, looked interesting.

    StuartR

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IP Address (Word 97SR2)

    Ok. Here's the latest. Query the Windows Management Instrumentation objects.
    This needs testing on Win9x.

    Also, it'll check all adapters but returns the IP address for the first one with
    IP enabled.

    Function IPAddress()
    Dim i As Integer
    Dim strComputer As String
    Dim oWMIService As Object
    Dim IPConfigSet As Object
    Dim IPConfig As Object

    strComputer = "."
    Set oWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")
    Set IPConfigSet = oWMIService.ExecQuery _
    ("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
    For Each IPConfig In IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
    For i = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
    IPAddress = IPConfig.IPAddress(i)
    Exit Function
    Next
    End If
    Next

    End Function
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  15. #15
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: IP Address (Word 97SR2)

    On my Windows98SE system this one gives "Run time error '432' File name or class name not found during Automation operation" for the line
    Set oWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

    StuartR

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •