使用一个会话进行多个子呼叫

问题描述 投票:0回答:1

我正在使用 2 个模块,现在我想将它们合并到一次运行中,因此我在代码中调用这两个模块。 但每个子程序都是为了独立运行而创建的,因此每个子程序都单独与 SAP 建立连接。 当调用子程序时,我总是需要确认“脚本想要访问 SAP GUI”。

当我第一次授予访问权限时,我在调用第二个模块时无法使用该访问权限。 在代码执行期间是否可以使用 1 个会话运行多个模块/子模块?

我尝试在代码开头使用函数,但无法使其工作。 有人知道如何正确地做到这一点吗? 我还是个新手,所以详细的解释对学习很有帮助:)

这是我的功能,但我仍然需要单独连接每个模块来运行特定的代码。

Function SAP_Connection() As Boolean

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 SAP_session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject Session, "on"
   WScript.ConnectObject SapApplication, "on"
End If

SAP_Connection = True


End Function
excel vba sapui5
1个回答
0
投票

首先,我会将这部分代码放在您拥有的每个 SAP 子组件的开头,而不是将其作为独立函数并调用它。

Sub SAP_procedure1()
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 SAP_session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject Session, "on"
   WScript.ConnectObject SapApplication, "on"
End If

Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
' insert rest of code

接下来,我将检查您的 SAP 可访问性和脚本设置。您可以关闭您提到的两个设置,通知/授予对 SAP GUI 连接的访问权限。 为此,请转至 SAP Easy Access 用户菜单 -> 选项 -> 辅助功能和脚本 -> 脚本 -> 取消选中以下复选框:“脚本附加到 SAP GUI 时通知”和“脚本打开连接时通知”

请参阅附图设置:

SAP Scripting Settings

然后根据需要组合这些程序。如果它们是两个不同的过程,则将它们组合起来的一种简单方法如下所示:

Sub a_combined()
Dim wbName As String:       wbName = ThisWorkbook.Name

Application.Run "'" & wbName & "'!SAP_procedure1"

Application.Run "'" & wbName & "'!SAP_procedure2"

End Sub

如果这有帮助或者您需要更多帮助,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.