简体   繁体   English

在 VBA 中运行时,用于数据导出的 SAP GUI VBScript 会导致空文档 [excel]

[英]SAP GUI VBScript for data export results in empty document when run in VBA [excel]

I have a VBScript that runs a data export from SAP GUI.我有一个运行从 SAP GUI 导出数据的 VBScript。 If executed alone or in SAP it runs and results in an excel worksheet with the desired data.如果单独执行或在 SAP 中执行,它会运行并生成包含所需数据的 Excel 工作表。

However, I want to have it implemented in a VBA Sub.但是,我想在 VBA Sub 中实现它。 I found this thread that was already very helpful.我发现这个线程已经非常有帮助了。 When I start the macro in excel, it runs the whole script through SAP without error only to open the destination Excel file without saving the data to it.当我在 excel 中启动宏时,它通过 SAP 运行整个脚本而不会出错,只是打开目标 Excel 文件而不将数据保存到其中。 At first, I didn't realize it, but when I clear the document it is obvious that it is not being overwritten.起初,我没有意识到,但是当我清除文档时,很明显它没有被覆盖。

Also there is no error message showing.也没有显示错误消息。

Thanks in advance for your help.在此先感谢您的帮助。

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

Edit:Typos编辑:错别字

By chance I ran into the same mistake with different consequences elsewhere.一个偶然的机会,我在其他地方遇到了同样的错误,但结果却不同。 Long story short: It seems you can't use %UserName% when operating from Excel.长话短说:从 Excel 操作时,您似乎无法使用 %UserName%。 Instead replaced it with this而是用这个替换了它

SAP_session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\" & Environ("username") & "\Documents\SAP\SAP GUI\"

Might help someone.可能会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM