Sap Scripting can be really tricky sometimes. My job is to help You and solve any mistery case. In this article I’m going to show You a trick, which will help You to get next SAP object in loop and get through the iteration issue.

So, I was looping through tabs of MM02 transaction and got stuck for couple of minutes on the line below.
Set sapTab = session.findById("wnd[0]/usr/tabsTABSPR1").Children.Item(i)
Bad index type for collection access
This line was at the beginning of the loop and stood for the one of 26 tabs of MM02 transaction. The error message, which I received looked like below:

Of course the i iterator was declared before.
Iterator variable type
Firstly, I declared that as Long type. I just got used to bigger natural numbers.
Dim i As Long
After some research I had the feeling, that it should be Integer, so I changed that.
Dim i As Integer
Unfortunately that did not solve this case.
I replaced the i with the simple numbers like 1 or 2 and everything was fine – the object was set with no error. I even tried with Strings , don’t know really why, but I was THAT clueless in that time.
Solution #1
So after some time, after many many struggles I tried to convert the iterator
Set sapTab = session.findById("wnd[0]/usr/tabsTABSPR1").Children.Item(CInt(i))
And it worked!
Solution #2
What I also found is that You can add the fixed number to the iterator and that also cause, that it allow us to use the iterator variable in setting the SAP objects.
Set sapTab = session.findById("wnd[0]/usr/tabsTABSPR1").Children.Item(i + 0)
And that was it! I can’t really explain that in details
(snippet from the looping through MM02 tabs article)
Dim i As Integer, sapTab As Object, tabName As String
For i = 0 To session.findById("wnd[0]/usr/tabsTABSPR1").Children.Count - 1
Set sapTab = session.findById("wnd[0]/usr/tabsTABSPR1").Children.Item(CInt(i))
tabName = Trim(sapTab.Text)
If tabName = "Sales: sales org. 1" Then
sapTab.Select
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTA").Text = "Z5"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTD").Text = "10.12.2021"
ElseIf tabName = "MRP 1" Then
sapTab.Select
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTA").Text = "Z5"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTD").Text = "10.12.2021"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2482/ctxtMARC-DISMM").Text = "ZX"
End If
Next