+ Reply to Thread
Results 1 to 14 of 14

Pickup time from Internet servers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Pickup time from Internet servers

    Hi all,

    Kindly refer to the attachment.

    Column C has a list of city names.. Is there a way we can pull the current time from any website or internet server.
    If not only the time zone they fall under

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Pickup time from Internet servers

    You're looking for something like the code below, I suspect.
    Set references to Microsoft XML and Microsoft HTML

    Sub excelforum()
    Dim TaD As MSXML2.XMLHTTP, TD As MSHTML.HTMLDocument, cell As Range
    Dim url$, country$, city$
    
    On Error GoTo earlyexit
    
    Set TaD = New MSXML2.XMLHTTP
    Set TD = New HTMLDocument
    url = "http://www.timeanddate.com/worldclock/"
    
    For Each cell In Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
    If Cells(cell.Row, 1) = "United States" Then country = "usa" Else country = LCase(Cells(cell.Row, 1))
    country = Replace(country, " ", "-")
    city = Replace(LCase(Cells(cell.Row, 3)), " ", "-")
    
    
    TaD.Open "GET", url & "/" & country & "/" & city, False
    TaD.send
    TD.body.innerhtml = TaD.responseText
    
    If InStr(TaD.responseText, "Unknown address") > 0 Then
    cell.Offset(0, 1) = "Couldn't find the time, sorry!"
    Else
    cell.Offset(0, 1) = TD.getElementById("qlook").innerText
    End If
    
    Next cell
    
    earlyexit:
    Application.ScreenUpdating = True
    End Sub
    Please click the * below if this helps
    Last edited by JasperD; 04-05-2015 at 06:25 AM. Reason: Forgot to implement the replace for spaces in countries
    Please click the * below if this helps

  3. #3
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    Hi Jasper.. thanks for the help..

    I did try running the code .. but it gave me an compile error message.

    Attached screenshot.. Kindly assist
    Attached Images Attached Images

  4. #4
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    ATTACHED output of the VBA.... we almos there...

    the D column is giving us result in non english, also, can we not just search with the City name if that helps for more results

    Kindly advise

  5. #5
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    it was the reference.. i was able to run it... checkin it now

  6. #6
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Pickup time from Internet servers

    You can't search with just city name.
    The result will always be in your system language, since that's how the website presents it.

    If you find another site that gives the result in English and is flexibile enough to search for the city AND find what you need, then feel free to let me know.
    But be careful with just looking for city, because if you'd look for "London", you'd get 4 hits in 3 different countries.

  7. #7
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    I agree to your feedback...

    Can we try http://www.thetimenow.com/

  8. #8
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Pickup time from Internet servers

    That site poses the exact same problems as the one I used.
    I don't really feel the necessity to change it around, but feel free to adjust the code to your liking.

    As long as you don't type the country / state / city names exactly as expected by the site, you'll always going to get misses.

  9. #9
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    Okay...
    Jasper, just wanted to get this clarified further.. When i execute this code.. I get unknown address for almost 40 city's.. But when I search for them in the website.. It works absolutely fine.. Are we missing anything here..

    Please advise..

  10. #10
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Pickup time from Internet servers

    This won't find the time for:

    "Oakville, Canada" = Doesn't exist on the site
    "Beyazit-Istanb" = "Istanbul"
    "Sharm el Sheik" = "Sharm ash Shaykh"
    "Moscow, Russian Federation" = "Moscow, Russia"
    "Victoria - Mah, Seychelles" = "Victoria, Seychelles"
    "Bangalore - India" = Doesn't exist on the site
    "Enfield, Canada" = Doesn't exist on the site

    etc.

  11. #11
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Pickup time from Internet servers

    Try

    Sub excelforum()
    Dim TaD As MSXML2.XMLHTTP, cell As Range
    Dim url$, city$, x&
    
    On Error GoTo earlyexit
    
    Set TaD = New MSXML2.XMLHTTP
    url = "http://www.thetimenow.com/suggest.php?location="
    
    For Each cell In Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
    city = Replace(LCase(Cells(cell.Row, 3)), " ", "+")
    x = 0
    
    TaD.Open "GET", url & city, False
    TaD.send
    
    If TaD.responseText = "[]" Then cell.Offset(0, 1) = "Couldn't find the time, sorry!": GoTo nxc:
    If Not cell.Offset(0, -1) = vbNullString And Not cell.Offset(0, -1) = "Unknown" Then x = InStr(TaD.responseText, cell.Offset(0, -1))
    If x > 0 Then cell.Offset(0, 1) = Mid(TaD.responseText, InStr(x, TaD.responseText, Chr(34) & "," & Chr(34)) + 3, 5): GoTo nxc
    x = InStr(TaD.responseText, cell.Offset(0, -2)) - 8
    If x > 0 Then cell.Offset(0, 1) = Mid(TaD.responseText, x, 5): GoTo nxc
    cell.Offset(0, 1) = "Couldn't find the time, sorry!"
    
    nxc:
    Next cell
    
    earlyexit:
    Application.ScreenUpdating = True
    End Sub
    Please click the * below if this helps

  12. #12
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Pickup time from Internet servers

    Gotta love Internet cache :-)
    Try

    Sub excelforum()
    Application.ScreenUpdating = False
    
    Dim TaD As MSXML2.XMLHTTP, cell As Range
    Dim url$, city$, x&
    
    On Error GoTo earlyexit
    
    Set TaD = New MSXML2.XMLHTTP
    url = "http://www.thetimenow.com/suggest.php?location="
    
    For Each cell In Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
    Application.ScreenUpdating = True
    cell.Activate
    DoEvents
    Application.ScreenUpdating = False
    
    city = Replace(LCase(Cells(cell.Row, 3)), " ", "+")
    x = 0
    
    TaD.Open "GET", url & city & "&" & CInt(1000 * Rnd()), False
    TaD.send
    
    If TaD.responseText = "[]" Then cell.Offset(0, 1) = "Couldn't find the time, sorry!": GoTo nxc:
    If Not cell.Offset(0, -1) = vbNullString And Not cell.Offset(0, -1) = "Unknown" Then x = InStr(TaD.responseText, cell.Offset(0, -1))
    If x > 0 Then cell.Offset(0, 1) = Mid(TaD.responseText, InStr(x, TaD.responseText, Chr(34) & "," & Chr(34)) + 3, 5): GoTo nxc
    x = InStr(TaD.responseText, cell.Offset(0, -2)) - 8
    If x > 0 Then cell.Offset(0, 1) = Mid(TaD.responseText, x, 5): GoTo nxc
    cell.Offset(0, 1) = "Couldn't find the time, sorry!"
    
    nxc:
    Next cell
    
    earlyexit:
    Application.ScreenUpdating = True
    End Sub

    Typing from phone, so untested
    Last edited by JasperD; 04-06-2015 at 11:15 AM. Reason: Tested it and included cell.activate to show where you are...

  13. #13
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    this works absolutely fine.. Just that after few min when I exceute same code again, it does not update. It gives the same result of the first execute

    I have to relaunch excel file and run the code again to get an updated time

  14. #14
    Forum Contributor
    Join Date
    06-12-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    153

    Re: Pickup time from Internet servers

    supe supee.. double cheerz to u... thanks a lot... that work perfect

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Real time spell check using internet in excel
    By drsantosh in forum Excel General
    Replies: 2
    Last Post: 08-20-2014, 11:53 AM
  2. Excel 2007 : Connecting to 2 SQL servers
    By blstrahan044 in forum Excel General
    Replies: 0
    Last Post: 06-23-2011, 09:35 AM
  3. use shared workbooks on Web servers
    By Gkrish in forum Excel General
    Replies: 1
    Last Post: 01-22-2005, 06:06 PM
  4. [SOLVED] Where're the good newgroups servers?
    By Nystas in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 01-19-2005, 09:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1