Results 1 to 3 of 3

Help with getting all elements in the XML node list

Threaded View

yoursamrit2000 Help with getting all... 02-14-2016, 04:16 PM
yoursamrit2000 Re: Help with getting all... 03-04-2016, 05:32 PM
yoursamrit2000 Re: Help with getting all... 12-23-2016, 08:28 PM
  1. #1
    Registered User
    Join Date
    09-27-2012
    Location
    Mumbai, IN
    MS-Off Ver
    Office 365
    Posts
    97

    Help with getting all elements in the XML node list

    Hi Guys,

    I know I have done this before but i cant locate that file now, but here's what i want help with.

    What i want is i have a userform with a textbox1 on it when i click command button 1, it should search the address ....like if i put 125 main st...i want a listbox to be dynamically added to the userform with all the possible formatted addresses listed in it...currently it only gives me one address

    
    Function G_LATLNG( _
        InputLocation As Variant, _
        Optional n As Long = 4, _
        Optional Requery As Boolean = False _
        ) As Variant
    ' Requires a reference to Microsoft XML, v6.0
    ' The parameter 'n' refers to the type of reponse
    ' n = 1: Returns latitude, longitude as string
    ' n = 2: Returns latitude as double
    ' n = 3: Returns longitude as double
    ' n = 4: Returns address as string
     
    ' Updated 30/10/2012 to
    '   - return an #N/A error if an error occurs
    '   - cache only if necessary
    '   - check for and attempt to correct cached errors
    '   - work on systems with comma as decimal separator
     
    Dim myRequest As XMLHTTP60
    Dim myDomDoc As DOMDocument60
    Dim addressNode As IXMLDOMNode
    Dim latNode As IXMLDOMNode
    Dim lngNode As IXMLDOMNode
    Dim statusNode As IXMLDOMNode
    Dim CachedFile As String
    Dim NoCache As Boolean
    Dim V() As Variant
        On Error GoTo exitRoute
        G_LATLNG = CVErr(xlErrNA) ' Return an #N/A error in the case of any errors
        ReDim V(1 To 4)
        
        ' Check and clean inputs
        If WorksheetFunction.IsNumber(InputLocation) _
            Or IsEmpty(InputLocation) _
            Or InputLocation = "" Then GoTo exitRoute
        InputLocation = URLEncode(CStr(InputLocation), True)
        
        ' Check for existence of cached file
        CachedFile = Environ("temp") & "\" & InputLocation & "_LatLng.xml"
        NoCache = (Len(Dir(CachedFile)) = 0)
        
        Set myRequest = New XMLHTTP60
        
        If NoCache Or Requery Then ' if no cached file exists or if asked to requery then query Google
            ' Read the XML data from the Google Maps API
            myRequest.Open "GET", "http://maps.googleapis.com/maps/api/geocode/xml?address=" _
                & InputLocation & "&sensor=false", False
            myRequest.Send
        Else ' otherwise query the cached file
            myRequest.Open "GET", CachedFile
            myRequest.Send
            Set myDomDoc = New DOMDocument60
            myDomDoc.LoadXML myRequest.responseText
            ' Get the status code of the cached XML file in case of previously cached errors
            Set statusNode = myDomDoc.SelectSingleNode("//status")
            If statusNode.Text <> "OK" Then
                Call G_LATLNG(InputLocation, n, True) ' Recursive way to try to remove cached errors
            End If
        End If
        
        ' Make the XML readable using XPath
        Set myDomDoc = New DOMDocument60
        myDomDoc.LoadXML myRequest.responseText
        
        ' If statusNode is "OK" then get the values to return
        Set statusNode = myDomDoc.SelectSingleNode("//status")
        If statusNode.Text = "OK" Then
            ' Get the location as returned by Google
           Set addressNode = myDomDoc.SelectSingleNode("//result/formatted_address")
            ' Get the latitude and longitude node values
            Set latNode = myDomDoc.SelectSingleNode("//result/geometry/location/lat")
            Set lngNode = myDomDoc.SelectSingleNode("//result/geometry/location/lng")
            V(1) = latNode.Text & "," & lngNode.Text
            V(2) = Val(latNode.Text) ' Fixed for systems with comma as decimal separator
            V(3) = Val(lngNode.Text) ' Fixed for systems with comma as decimal separator
            V(4) = addressNode.Text
            G_LATLNG = V(n)
            If NoCache Then: Call CreateFile(CachedFile, myRequest.responseText) ' Cache API response if required
        End If
     
    exitRoute:
        ' Tidy up
        Set latNode = Nothing
        Set lngNode = Nothing
        Set myDomDoc = Nothing
        Set myRequest = Nothing
    End Function
    
    
    Public Function URLEncode( _
       StringVal As String, _
       Optional SpaceAsPlus As Boolean = False _
    ) As String
    ' Function from http://www.tinyguru.com/error/qid218181.html
    Dim StringLen As Long: StringLen = Len(StringVal)
    Dim i As Long, CharCode As Integer
    Dim Char As String, Space As String
    
        If StringLen > 0 Then
            ReDim result(StringLen) As String
    
        If SpaceAsPlus Then Space = "+" Else Space = "%20"
    
            For i = 1 To StringLen
                Char = Mid$(StringVal, i, 1)
                CharCode = Asc(Char)
                Select Case CharCode
                    Case 97 To 122, 65 To 90, 48 To 57, _
                        45, 46, 61, 95, 123, 125, 126
                        result(i) = Char
                    Case 32
                        result(i) = Space
                    Case 0 To 15
                        result(i) = "%0" & Hex(CharCode)
                    Case Else
                        result(i) = "%" & Hex(CharCode)
                End Select
            Next i
            URLEncode = Join(result, "")
        End If
    End Function
    '========================================================================================
    Function CreateFile(fileName As String, Contents As String) As String
    ' Function from http://www.jpsoftwaretech.com/vba/create-new-text-documents-using-vba/
    ' creates file from string contents
    Dim tempFile As String
    Dim nextFileNum As Long
      nextFileNum = FreeFile
      tempFile = fileName
      Open tempFile For Output As #nextFileNum
      Print #nextFileNum, Contents
      Close #nextFileNum
      CreateFile = tempFile
    End Function
    Last edited by yoursamrit2000; 02-14-2016 at 05:35 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Get Node Key from selected Node in TreeView
    By mile990 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2016, 04:55 PM
  2. AutoComplete Drop-Down LIST with sub elements
    By abdulla07 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2013, 08:01 AM
  3. Resources for Using VBA to Manipulate Silverlight Elements like HTML Elements
    By linear_db in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-16-2013, 04:43 PM
  4. Randomly add List Elements to Chart
    By cnicholas in forum Excel General
    Replies: 1
    Last Post: 07-01-2008, 03:09 PM
  5. [SOLVED] How to format two repeating XML elements w/o getting list of list
    By jimmyray32 in forum Excel General
    Replies: 0
    Last Post: 11-10-2005, 11:20 AM
  6. [SOLVED] Make a list from a table of elements ?
    By Lucy Lastic in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 09:10 AM
  7. hidden elements from a list
    By nadeaurachel in forum Excel General
    Replies: 0
    Last Post: 03-04-2005, 01:26 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