Results 1 to 6 of 6
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Can a VBScript check the internet connection? (WinXP SP-2)

    I know there's an API function for checking the PC's internet-connected state, but VBScripts can't access the API.

    Does anyone know if there's a relatively straightforward way for a VBScript to check if the user is connected to the internet?

  2. #2
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Can a VBScript check the internet connection?

    First thing I thought when I read your enquiry was "Could you PING a website?" That would certainly tell if there is a connection.

    I found this example:

    http://www.robvanderwoude.com/vbstec...t_website.html

    Would that do?
    Jerry

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Can a VBScript check the internet connection?

    Thanks for the reply. I'd actually already looked at that very example, but concluded it presented more complications than I wanted to try to work through -- e.g., how to adjust the CreateObject and SetRequestHeader lines for WinXP and IE7. Plus I was hoping for a straightforward "am I connected" approach, rather than a "can I connect to a particular site" approach. But your post prompted me to give van der Woude's code another look, and it turned out I probably didn't need to do any tweaking at all. (I changed Mozilla to IE, but left the 5.1 references the same, and it works.) <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

  4. #4
    New Lounger
    Join Date
    Jun 2006
    Location
    Olympia, Washington, USA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can a VBScript check the internet connection?

    This script does the trick. It looks through all of your network connections; if one is active then it returns a connected value.
    The only thing that would be tricky is if you had several network connections (like an ethernet and a wireless card) and you wanted to look at the connectivity for a particular adapter. Easy to modify, but something to keep in mind.

    Have a great day.

    '----------------------------
    On Error Resume Next
    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20
    strComputer = "."
    bolActiveConnection = False
    Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkConnection", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
    For Each objItem In colItems
    If objItem.ConnectionState = "Connected" Then
    bolActiveConnection = True
    End If
    Next
    'Instead of displaying the output here you could do something interesting
    'like If bolActiveConnection = True then do some function
    Wscript.Echo "Computer connected = " & bolActiveConnection

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Can a VBScript check the internet connection?

    Thanks for jumping in, and for introducing me to the NetworkConnection collection. Unfortunately, on my PC (which is non-networked and running XP-SP2), bolActiveConnection ends up True whether or not I'm connected to the internet.

    I inserted MsgBox "Testing one" right after the For line, and inserted MsgBox "Found one" right after the If line, and each MsgBox shows up once (and only once) when I run the script. I accordingly conclude that there's only one item in my collection of network connections -- and for some reason its ConnectionState is "Connected" whether or not I'm connected to the internet. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Can a VBScript check the internet connection?

    I should have mentioned that I'm dial-up -- in case that's relevant.

Posting Permissions

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