尝试连接到 SAP 时出现 运行-时间错误“91”
Getting Run-Time error '91' when trying to connect to SAP
我正致力于通过 VBA 连接到 SAP GUI,但无法弄清楚如何在它自己的函数中建立连接。这是假设您已经登录到 SAP GUI。我收到“未设置对象变量或块变量”错误。
这是一个有效的例子...
Sub runProgram()
'Connet to SAP."sapguiapp", used to be named "Application" which is an excel keyword
If Not IsObject(sapguiapp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapguiapp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sapguiapp.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 sapguiapp, "on"
End If
'Maximize the SAP window
session.findById("wnd[0]").maximize
' Do a whole bunch of other SAP work, now that we're connected
'....
End Sub
但是,我想将连接部分拆分成它自己的部分 sub/function 以将其与实际工作分开。我以为我可以简单地创建一个新函数并将 return 'session' 作为对象。
这行不通:
Sub runProgram()
'Call the function that connects to SAP
Dim session As Object
Set session = ConnectToSAP
'Maximize the SAP window
session.findById("wnd[0]").maximize
' Do a whole bunch of other SAP work, now that we're connected (JK, we're not...)
'....
End Sub
Function ConnectToSAP() As Object
'Connet to SAP."sapguiapp", used to be named "Application" which is an excel keyword
If Not IsObject(sapguiapp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapguiapp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sapguiapp.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 sapguiapp, "on"
End If
End Function
有什么明显的我遗漏的东西吗?我有几个连接到 SAP 的潜艇,所以我想拆分出实际的连接代码。任何提示将不胜感激!
编辑:请注意,我没有从我的实际程序中 copy/paste 这样做,所以如果有一个小错字,我深表歉意。请发表评论让我知道我哪里搞砸了:)
在 ConnectToSAP
函数中,您缺少末尾的赋值语句 Set ConnectToSAP = session
。
我建议使用 Option Explicit
并正确声明变量。
如果您添加对 sapfewse.ocx
的引用(位于您的 SAP 安装文件夹 C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewse.ocx
中),您甚至可以使用 VBAs intellisense。
如果您不想添加引用,请使用 Object
而不是 SAPFEWSLib.GuiSession
、SAPFEWSLib.GuiConnection
和 SAPFEWSLib.GuiApplication
.
Option Explicit
Function ConnectToSAP() As SAPFEWSELib.GuiSession
Dim SapGuiAuto As Object
Dim SapGuiApp As SAPFEWSELib.GuiApplication
Dim connection As SAPFEWSELib.GuiConnection
On Error GoTo Err1
Set SapGuiAuto = GetObject("SAPGUI")
Set SapGuiApp = SapGuiAuto.GetScriptingEngine
On Error GoTo Err2
Set connection = SapGuiApp.Connections(0)
Set ConnectToSAP = Connection.Sessions(0)
Exit Function
Err1:
MsgBox "Please launch SAP"
Exit Function
Err2:
MsgBox "Please login to SAP"
End Function
WScript
是一个 vbscript object and does not exist in vba。
它的 ConnectObject
方法用于事件处理,可以在 VBA 中完成而不需要 WScript
对象,尽管你确实需要一个 class 模块并声明你的会话变量 WithEvents
:
Class模块:
Private WithEvents session As SAPFEWSELib.GuiSession
Private requestCounter As Long
Private Sub session_startRequest(session As SAPFEWSELib.GuiSession)
requestCounter = requestCounter + 1
End Sub
我正致力于通过 VBA 连接到 SAP GUI,但无法弄清楚如何在它自己的函数中建立连接。这是假设您已经登录到 SAP GUI。我收到“未设置对象变量或块变量”错误。
这是一个有效的例子...
Sub runProgram()
'Connet to SAP."sapguiapp", used to be named "Application" which is an excel keyword
If Not IsObject(sapguiapp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapguiapp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sapguiapp.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 sapguiapp, "on"
End If
'Maximize the SAP window
session.findById("wnd[0]").maximize
' Do a whole bunch of other SAP work, now that we're connected
'....
End Sub
但是,我想将连接部分拆分成它自己的部分 sub/function 以将其与实际工作分开。我以为我可以简单地创建一个新函数并将 return 'session' 作为对象。
这行不通:
Sub runProgram()
'Call the function that connects to SAP
Dim session As Object
Set session = ConnectToSAP
'Maximize the SAP window
session.findById("wnd[0]").maximize
' Do a whole bunch of other SAP work, now that we're connected (JK, we're not...)
'....
End Sub
Function ConnectToSAP() As Object
'Connet to SAP."sapguiapp", used to be named "Application" which is an excel keyword
If Not IsObject(sapguiapp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapguiapp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sapguiapp.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 sapguiapp, "on"
End If
End Function
有什么明显的我遗漏的东西吗?我有几个连接到 SAP 的潜艇,所以我想拆分出实际的连接代码。任何提示将不胜感激!
编辑:请注意,我没有从我的实际程序中 copy/paste 这样做,所以如果有一个小错字,我深表歉意。请发表评论让我知道我哪里搞砸了:)
在 ConnectToSAP
函数中,您缺少末尾的赋值语句 Set ConnectToSAP = session
。
我建议使用 Option Explicit
并正确声明变量。
如果您添加对 sapfewse.ocx
的引用(位于您的 SAP 安装文件夹 C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewse.ocx
中),您甚至可以使用 VBAs intellisense。
如果您不想添加引用,请使用 Object
而不是 SAPFEWSLib.GuiSession
、SAPFEWSLib.GuiConnection
和 SAPFEWSLib.GuiApplication
.
Option Explicit
Function ConnectToSAP() As SAPFEWSELib.GuiSession
Dim SapGuiAuto As Object
Dim SapGuiApp As SAPFEWSELib.GuiApplication
Dim connection As SAPFEWSELib.GuiConnection
On Error GoTo Err1
Set SapGuiAuto = GetObject("SAPGUI")
Set SapGuiApp = SapGuiAuto.GetScriptingEngine
On Error GoTo Err2
Set connection = SapGuiApp.Connections(0)
Set ConnectToSAP = Connection.Sessions(0)
Exit Function
Err1:
MsgBox "Please launch SAP"
Exit Function
Err2:
MsgBox "Please login to SAP"
End Function
WScript
是一个 vbscript object and does not exist in vba。
它的 ConnectObject
方法用于事件处理,可以在 VBA 中完成而不需要 WScript
对象,尽管你确实需要一个 class 模块并声明你的会话变量 WithEvents
:
Class模块:
Private WithEvents session As SAPFEWSELib.GuiSession
Private requestCounter As Long
Private Sub session_startRequest(session As SAPFEWSELib.GuiSession)
requestCounter = requestCounter + 1
End Sub