Switching page numbers during XML import using VBA

SaveSquirrels

New Member
Joined
Jul 26, 2012
Messages
29
Hi,

I've been programming my VBA script to import data from an XML file which looks like this:

HTML:
<response responseStatus="success"> <result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:type="apiPagination"> <currentPage>1</currentPage> <list xsi:type="apiReport"> <Variable1>Name1</Variable1> <Variable2>41</Variable2> </list> <list xsi:type="apiReport"> <Variable1>Name2</Variable1> <Variable2>20</Variable2> </list>


So far everything works great and it imports everything correctly to Excel 2010. This is the code I'm using:


Code:
Sub test()
    
    Dim WS As Worksheet: Set WS = ActiveSheet
    Dim req As New XMLHTTP
    Dim Link As String
    Dim PageNumber As Integer
    Dim Url As String
         
    PageNumber = 1
    Link = "https://myserver.com/services/rest/g...rts/mypassword/"
    Url = Link & PageNumber
    
    req.Open "GET", Url, False
    req.Send
    
    
    Dim Resp As New DOMDocument
    Resp.LoadXML req.responseText
    
    Dim i As Integer
    Dim list As IXMLDOMNode
    
    For Each list In Resp.getElementsByTagName("list")
    i = i + 1
    WS.Range("A1:A100").Cells(i, 1).Value = list.SelectNodes("Variable1")(0).Text
    Next list
     
 End Sub

The problem is that I only import page1 :( (only link https://myserver.com/services/rest/g...rts/mypassword/1)

As you can see I've started to define the url so that other page numbers are possible. How do I get all URLs so that it also imports the data located on other pagenumbers? For example https://myserver.com/services/rest/g...rts/mypassword/2
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Never thought that I would solve it but i did (in only two hours) :)

Code:
        Dim AmountofPages As String    
    
        For Each result In Resp.getElementsByTagName("result")
        AmountofPages = result.SelectNodes("nbPages")(0).Text
        Next result
   
          
        For i2 = 1 To AmountofPages
        PageNumber = PageNumber + 1
        Url = Link & PageNumber
        req.Open "GET", Url, False
        req.Send
        Resp.LoadXML req.responseText
        For Each list In Resp.getElementsByTagName("list")
        i = i + 1
        WS.Range("C2:C500").Cells(i, 1).Value = list.SelectNodes("variable1")(0).Text
        Next list
             
       Next i2
 
Upvote 0

Forum statistics

Threads
1,216,146
Messages
6,129,142
Members
449,488
Latest member
qh017

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top