Search code examples
for-loopvbscriptasp-classic

How to extract only a part of "For Each" loop in ASP/VBScript


I was using this code to extract all items in a RSS file.

For Each item In r.items.items
    response.write(item.title)
Next

Each RSS file have 100 items and now I want to show only child numbers from 10 to 20. I dont know how to extract exact items. I have tried the following code:

For x = 10 To 20
    response.write(r.items.items(x).title)
Next

but I got an error:

Object not a collection: 'items'


Solution

  • Check the type of r.items.items. I'd suspect that it's enumerable, but doesn't allow indexed access to its elements. You can work around that by using a For Each loop with a custom counter.

    i = 0
    For Each item In r.items.items
        If i > 20 Then
            Exit For
        ElseIf i >= 10 Then
            response.write(item.title)
        End If
        i = i + 1
    Next