【发布时间】:2016-08-13 14:31:25
【问题描述】:
几周前,我通过 SAP 的内置 GUI 脚本功能生成了一个脚本,然后将 vba 放入一个 excel 文档并将其附加到一个按钮上。
这几个星期都很好,但是现在当我按下按钮时,我得到了这个错误:
"The enumerator of the collection cannot find en element with the specified index." 在写着Set session = Connection.Children(0) 的那一行
这是一段sn-p代码
If Not IsObject(Sap_Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Sap_Application.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0) 'This is the line that causes the error
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Sap_Application, "on"
End If
我已打开 SAP 并已登录,并且 sn-p 中的所有代码都是由 SAP gui 脚本生成的。
我尝试了一些我在网上看到的解决方案(比如将我的 sap 安装文件夹中的一些 ocx 文件添加到我的 excel 插件中),但似乎没有任何效果。
我想知道这里是否有人遇到过类似的问题,并且知道如何在我的 vba 中成功打开 SAP 会话。
从评论中编辑:
当我检查连接对象时,这就是我所看到的。看起来儿童长度为 0。
【问题讨论】:
-
如果无法建立连接,
Set Connection = Sap_Application.Children(0)行会抛出错误吗?您可以使用调试器、Watches 窗口来检查您获得的对象。你应该可以点击Connection.Children -
我在执行错误的行之前截取了调试器的屏幕截图
-
所以看起来 Children 是空的,但我不确定为什么会这样。我是否错误地打开了 SAP?