我有一个从SAP运行数据导出的VBScript。如果单独执行或在SAP中执行,它将运行并生成包含所需数据的excel工作表。
但是,我希望在VBA Sub中实现它。我发现this线程已经非常有用了。当我在excel中启动宏时,它通过SAP运行整个脚本,没有错误,只是打开目标Excel文件,而不将数据保存到其中。起初,我没有意识到这一点,但当我清除文档时,很明显它没有被覆盖。
此外,也没有显示错误消息。
提前感谢您的帮助。
Public Sub Connect_To_SAP()
On Error GoTo Err_NoSAP
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(SAP_session) Then
Set SAP_session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject SAP_session, "on"
WScript.ConnectObject SAPGuiApp, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").Maximize
On Error GoTo Err_Description
SAP_session.findById("wnd[0]").Maximize
SAP_session.findById("wnd[0]/tbar[0]/btn[12]").press
SAP_session.findById("wnd[0]/tbar[0]/btn[12]").press
SAP_session.findById("wnd[0]/tbar[0]/btn[12]").press
SAP_session.findById("wnd[0]/tbar[0]/okcd").Text = "Execution"
SAP_session.findById("wnd[0]/tbar[0]/btn[0]").press
SAP_session.findById("wnd[0]/tbar[1]/btn[8]").press
SAP_session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, ""
SAP_session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
SAP_session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
SAP_session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
SAP_session.findById("wnd[1]/tbar[0]/btn[0]").press
SAP_session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\%UserName%\Documents\SAP\SAP GUI\"
SAP_session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Test.XLSX"
SAP_session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
SAP_session.findById("wnd[1]/tbar[0]/btn[11]").press
Exit Sub
Err_Description:
MsgBox ("The program has generated an error;" & Chr(13) & _
"the reason for this error is unknown."), vbInformation
Exit Sub
Err_NoSAP:
MsgBox ("You don't have SAP open or " & Chr(13) & _
"scripting has been disabled."), vbInformation
Exit Sub
Err_TooManySAP:
MsgBox ("You must only have one SAP session open. " & Chr(13) & _
"Please close all other open SAP sessions."), vbInformation
Exit Sub
End Sub
编辑:打字错误
发布于 2021-11-09 05:08:36
偶然的是,我遇到了同样的错误,但在其他地方产生了不同的后果。长话短说:在Excel中操作时,似乎不能使用%UserName%。取而代之的是这个
SAP_session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\" & Environ("username") & "\Documents\SAP\SAP GUI\"
也许能帮到什么人。
https://stackoverflow.com/questions/69894120
复制