[英]SAP GUI Scripting in Excel VBA - Error 91 (Object Variable...)
I'm trying to connect to SAP through Excel VBA to run a recorded macro.我正在尝试通过 Excel VBA 连接到 SAP 以运行录制的宏。
When it reaches the start of actual SAP code it places the当它到达实际 SAP 代码的开头时,它会将
Run-Time Error '91'运行时错误“91”
on the line session.FindById("wnd[0]").Maximize
.在session.FindById("wnd[0]").Maximize
线上。
If I delete that line it has the same issues with every session line.如果我删除该行,则每个会话行都会出现相同的问题。
I verified the references and declarations.我验证了引用和声明。
I ran it as a VBS script and it worked.我将它作为 VBS 脚本运行,并且运行良好。
I'm logged into a SAP session before running the code.在运行代码之前,我已登录到 SAP 会话。
appl
contains Nothing in the value of the Local Windows. appl
在本地 Windows 的值中不包含任何内容。 On the Type it contains GuiApplication.在类型上,它包含 GuiApplication。
Here is the snip of the code:这是代码的片段:
Private Sub CommandButton1_Click()
Dim Tablename As String
If TextBox1 = "" Then
If Not IsObject(appl) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = appl.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 Application, "on"
End If
session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "Stock01"
session.FindById("wnd[0]/tbar[0]/btn[0]").press
session.FindById("wnd[0]/usr/ctxtS_MATNR-LOW").Text = "566666"
session.FindById("wnd[0]/usr/ctxtS_MATNR-HIGH").Text = "5666666"
session.FindById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = "1111"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").Text = "1045"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").SetFocus
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").caretPosition = 4
session.FindById("wnd[0]/tbar[1]/btn[8]").press
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").selectContextMenuItem "&XXL"
session.FindById("wnd[1]/tbar[0]/btn[0]").press
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Documents\SAP_GUI_Code"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Stock.XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
session.FindById("wnd[1]/tbar[0]/btn[11]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
exit Sub
You say that appl
contains Nothing.你说appl
包含任何东西。 So it's normal that session
also contains Nothing, hence the error 91 when the variable is used.因此session
也包含 Nothing 是正常的,因此使用该变量时会出现错误 91。
As @GSerg said in the conversation:正如@GSerg 在对话中所说:
That would be because IsObject determines if the variable has an object type, not whether it contains an instance of an object.那是因为 IsObject 确定变量是否具有对象类型,而不是它是否包含对象的实例。 In particular, IsObject(Nothing) is True.特别是,IsObject(Nothing) 是 True。 You want to replace all your IsObject calls with Is Nothing tests.您想用 Is Nothing 测试替换所有 IsObject 调用。
Consequently, the first lines should be:因此,第一行应该是:
If appl Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Connection Is Nothing Then
Set Connection = appl.Children(0)
End If
If session Is Nothing Then
Set session = Connection.Children(0)
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.