使用循环变量分配 Item-ID

Assign Item-ID with loop variable

我正在尝试编写一些 .vbs 脚本来减轻我的 SAP 工作量。 我开始很容易,所以首先我想看看在特定连接上打开了多少个会话。

这是我的代码:

Set SapGuiAuto  = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
iConnections = application.Connections.Count


If iConnections > 0 Then
    For i = 0 to iConnections - 1 
    iSessions = application.Connections.Item(i).Sessions.Count
    msgbox iSessions & "   Sessions for Connection " & i + 1
    Next
End If

问题是:
我在第 3 行遇到错误

Bad index type for collection access.

如果我只是在其中放一个 0 或 1 而不是 i,它就可以正常工作。但是我找不到带有变量的项目。

你们谁能帮帮我吗?我不知道该怎么办。

Connections 集合的索引可能是从 1 而不是 0 开始的。您可以尝试:

If iConnections > 0 Then
    For i = 1 to iConnections
    iSessions = application.Connections.Item(i).Sessions.Count
    msgbox iSessions & "   Sessions for Connection " & i 
    Next
End If

如果这不起作用,那么您可以尝试使用 For..Each 来枚举它们,而不是通过索引引用连接,就像这样

If iConnections > 0 Then
    For Each con in Application.Connections
    i = i + 1
    iSessions = con.Sessions.Count
    msgbox iSessions & "   Sessions for Connection " & i 
    'EDIT: The next line can't explicitly use Next con in VB-script, so I've commented out con
    Next 'con
End If

进一步补充汤姆在我上面的回答中的评论。我的第一个答案将使用 For Each..Next 方法,但我确实在 Next 语句中发现了语法错误(请参阅上面我对该行的编辑。

但是,如果您真的不想使用 For Each 方法,或者它不适合您,那么您应该能够使索引方法起作用。

VBScript 不是强类型的,因此您不能使用精确的数字类型(如 Integer、Long 或 Byte)对变量进行 Dim。但是,您正在使用的类型库可能需要 Connections 方法的强类型参数。通常,VBScript 或您调用的方法会处理此问题,但您确实提到您正在使用 SAP(我过去已经自动化),所以我假设这是问题的原因.

正如我所说,VBScript 不提供强类型声明,但它确实允许您将值强制转换为特定类型。你说当你提供像 0 或 1 这样的数字文字时你设法让代码工作,所以 似乎 你的连接方法将接受一个整数,但是你的 i 变量 可能 隐含地是一个 Long,考虑到它是如何声明的。

您可以通过将数字文字(默认为整数类型)分配给 i 来检查这一点,如下所示:

i = 0
iSessions = application.Connections.Item(i).Sessions.Count
msgbox iSessions & "   Sessions for Connection " & i 

有时,COM 对象不允许您在没有父成员的完整句柄的情况下访问子成员,因此更详细的测试是:

i = 0
Set con = application.Connections.Item(i)
iSessions = con.Sessions.Count
msgbox iSessions & "   Sessions for Connection " & i 

如果以上任何一项都有效,那么您就知道 Connections.Item 方法需要一个整数。所以你可能会让这个工作:

Set SapGuiAuto  = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine

'The connections.Count method might coerce a Long or an Integer, or even a Byte
iConnections = application.Connections.Count

If iConnections > 0 Then
    'Convert iConnections to an Integer, and i should then be an Integer too
    For i = 0 to CInt(iConnections) - 1
    set con = application.Connections.Item(i)
    iSessions = con.Sessions.Count
    msgbox iSessions & "   Sessions for Connection " & i + 1
    Next
End If

我设法避免坏索引错误的唯一方法是当我通过在每个变量后添加 +1+0-1 来操作相同的索引值时。

下面是使用早期绑定计算 1 个连接中的会话数

Sub Session_count()
        
Dim sapAuto As Object
        Dim sapGUI As SAPFEWSELib.GuiApplication
        Dim con As SAPFEWSELib.GuiConnection
        Dim sapSession As SAPFEWSELib.GuiSession
        Dim i As Long
        Dim iSessions As Long
        Dim IConnections As Long
        
        Set sapAuto = GetObject("sapgui")
        Set sapGUI = sapAuto.GetScriptingEngine
        
        IConnections = sapGUI.Connections.Count
        
        If IConnections > 0 Then
                Set con = sapGUI.Connections.Item(IConnections - 1 + 1 - 0 - 1)
                iSessions = con.Sessions.Count - 1
                If iSessions > 0 Then
                        i = 0
                        Do
                                Set sapSession = con.Children.Item(i + 1 + 0 - 1)
                                With sapSession
                                        Debug.Print .Name, .Type, .ID
                                End With
                                i = i + 1 + 1 - 0 - 1 'Here am still incementing i by 1 but for some reason SAP won't give you a bad index
                                Set sapSession = Nothing
                        Loop While i + 0 + 1 - 1 < iSessions + 1
                End If
        End If
End Sub

由于post中还有activity,我将post我的最终代码。它不再使用了,但我认为,如果仍然有人在寻找该特定问题的答案,那么这里有一个可行的解决方案。 (虽然其他答案真的很有帮助!)

这是通过在 .hta 文件中运行的脚本编写的。因此列表框。所有会话都在其中列出了它们的 ID。

function SessionInfo    

Dim i as Long

        'clear listbox
        SessionList.innerhtml = ""

        'create listbox
        Set optGroup = Document.createElement("OPTGROUP")
        optGroup.label = "Server"

        'count number of connections
        ConnectionCount = application.Connections.Count

        If ConnectionCount > 0 Then
            Sessionlist.appendChild(optGroup)
        Else 
            optGroup.label = No connection."
            Sessionlist.appendChild(optGroup)           
        End If

        If ConnectionCount > 0 Then
            For Each conn in application.Connections
                SessionCount = conn.Sessions.Count
                Set objOption = nothing
                Set optGroup = Document.createElement("OPTGROUP")
                optGroup.label = conn.Description
                Sessionlist.appendChild(optGroup)
                i = 0
                For Each sess In conn.Sessions
                    i = i + 1
                    Set objOption = Document.createElement("OPTION")
                    objOption.Text = "Session " & i & ": " & sess.ID
                    objOption.Value = sess.ID
                    SessionList.options.add(objOption)
                Next
            Next
        Else
            Exit function
        End If

End function