gpt4 book ai didi

excel - VBA Sap Scripting 打开或连接/保留在已打开的 session 窗口中

转载 作者:行者123 更新时间:2023-12-04 21:50:24 25 4
gpt4 key购买 nike

我可以通过使用 VBA 和 SAP 记录和播放 .vbs 脚本来自动化 SAP 事务数据检索,现在我遇到了一个我在 Visual Basic Studio .NET 的同一程序中没有看到的问题,如果我的 SAP Easy Access名称连接:LA-6-Prod 已经打开,然后出现 Multilogon 窗口并破坏我的代码,从而使整个事情失败。

如何让我的代码使用已经存在且打开的连接,或者如果它未打开,则打开它?

如果 LA-6-Prod 未打开,它可以完美运行,但无法告诉最终用户在执行宏之前确保没有打开连接窗口。

非常感谢

这是我的代码:

Sub my_sap
Dim SapGui, Applic, connection, session, WSHShell

strconnection = Worksheets("sap_info").Range("A2").Value
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
Set WSHShell = CreateObject("WScript.Shell")

Do Until WSHShell.AppActivate("SAP Logon ")
application.Wait Now + TimeValue("0:00:01")
Loop

Set WSHShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Applic = SapGui.GetScriptingEngine

Set connection = Applic.OpenConnection('LA-6-Prod', True)
Set session = connection.Children(0)

'===============SAP SCRIPTING===========

'Execute SAP stuffs, I put in here what I recorded from sap record and it works OK

'=======================================

Set session = Nothing
connection.CloseSession ("ses[0]")
Set connection = Nothing
Set sap = Nothing

End Sub

最佳答案

您可以使用一个函数来测试连接是否已经打开,使用类似这样的东西......

Sub sap()

If SAP_Connection Then
MsgBox ("Sap is Open so just attached to session(0)")
Else
MsgBox ("Sap is NOT open so open Logon Window")
End If

End Sub


该功能看起来像这样......
Function SAP_Connection() As Boolean

On Error GoTo ErrSap

If Not IsObject(SapApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SapApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SapApplication.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 SapApplication, "on"
End If

SAP_Connection = True
Exit Function

ErrSap:
SAP_Connection = False

End Function

关于excel - VBA Sap Scripting 打开或连接/保留在已打开的 session 窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55733193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com