在 excel/VBA 中使用 SAPGUI,为什么更改 GuiTableControl 的 .VerticalScrollbar.Position 会破坏对象 "connection"?

Using SAPGUI in excel/VBA, Why does changing the .VerticalScrollbar.Position of a GuiTableControl breaks the object "connection"?

首先是我的测试代码

Sub mytest2()
If Not IsObject(MyApplication) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set MyApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = MyApplication.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(Wscript) Then
   Wscript.ConnectObject session, "on"
   Wscript.ConnectObject MyApplication, "on"
End If


Dim MyTableStr As String
Dim MyTable As Variant

MyTableStr = "wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpVGUE/ssubSUB_AUFTRAG:SAPLCOVG:3010/tblSAPLCOVGTCTRL_3010"
Set MyTable = session.findById(MyTableStr)
MyTable.VerticalScrollbar.Position = MyTable.VerticalScrollbar.Position + 1
Debug.Print MyTable.VerticalScrollbar.Position & " position"

此代码将增加 table 的垂直位置,然后在 debug.print 行失败并出现错误 "Object doesn't support this action" 运行-时间错误 445

如果我再次添加行

Set MyTable = session.findById(MyTableStr)

在 .position 行和 debug.print 行之间,它起作用并打印位置。

为什么会这样?

我在 SAP GUI 脚本 API pdf 中找到了答案 http://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf

重要的段落在 GuiButton 控件的描述中。

Function press

This emulates manually pressing a button. Pressing a button will always cause server communication to occur, rendering all references to elements below the window level invalid. The following code will therefore fail:

Set TextField = session.findById(".../txtF1") 
session.findById(".../btnPB5").press 
TextField.text = "Hello"

这是因为当您执行命令时,有些命令只会影响客户端状态,而其他命令会导致与服务器的通信,显然当发生这种情况时,所有先前的对象引用都会被破坏。

更改 GuiTableControl 的 .VerticalScrollbar.Position 也会导致与服务器通信。