on 02-26-2008 1:28 PM
Hi there,
I am writing a script in VBA to connect to SAP.
I encountered something strange.
Why does this work:
Set oSession = oConnection.Children(0)
But this isn't:
X = 0
Set oSession = oConnection.Children(X)
It results in an error: "Bad index type for collection access"
Regards, Bas Prins
Maybe you need to do this...
DIM X AS INTEGER = 0
Set oSession = oConnection.Children(X)
Greetings,
Blag.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry to revive this old thread. I came across a similar issue today and found an (odd) solution, which I'm sharing in case it can help anyone else in the future.
The collection index needs to be a LONG, not an INTEGER. For some mysterious reason even if you declare it as a LONG it still won't work. But if you also cast it to a long, then it works.
DIM X AS LONG
X=0
Set oSession = oConnection.Children(Clng(X))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
To extend the good answer, the following works (tests in VBA Office 64 bits with SAP GUI 7.60):
Set oSession = oConnection.Children((x)) ' Pass by value instead of passing by reference
Set oSession = oConnection.Children(CInt(x))
Set oSession = oConnection.Children(CLng(x))
Set oSession = oConnection.Children.ElementAt(x)
This works also for a fixed number:
Set oSession = oConnection.Children(0)
Set oSession = oConnection.Children.ElementAt(0)
Set oSession = oConnection.Children.Item(0)
This doesn't work ("Run-time error '618': Bad index type for collection access" or "Run-time error '618': Application-defined or object-defined error"):
Set oSession = oConnection.Children.Item(x)
Note that I don't have any issue with VBScript, everything works fine.
Use a calculation to determine the index.
Example:
Dim X As Integer, oGuiApplication As Object, oConnection As Object, Session As Object
X = 0
Set Session = oConnection.children(0 + X)
- or -
Set oConnection = oGuiApplication.children(oGuiApplication.Connections.Count - 1)
Thanks,
Cas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.