我希望你能用下面的代码帮助我。我正在尝试每天通过 VBA 使用记录的 SAP GUI 脚本从 SAP 导出报告。随后,我想将数据复制到另一个工作簿并对其进行一些重新格式化/计算。但是,只有在我完全完成宏后,excel 导出文件才会打开。
使用例如 ShellAndWait http://www.cpearson.com/excel/ShellAndWait.aspx假设它是一个外部应用程序,但我想找到一个解决方案,我可以将所有内容保存在一个文件中。
提前感谢你的帮助!
编辑:解决了!感谢 Scriptman。
出口投资
Public Sub ExportInvest()
Dim Today As String
Dim FileName As String
Dim Ret
Today = Format$(DateTime.Now, "yyyymmdd__hh-MM-ss")
FileName = "Invest_" & Today & ".xml"
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzx03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtENAME-LOW").Text = "TXO4074"
session.findById("wnd[1]/usr/txtENAME-LOW").SetFocus
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[1]").Select
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "S:\FL_DMA\SAP Scripts\test\backup"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = FileName
session.findById("wnd[1]/tbar[0]/btn[0]").press
Set wshell = CreateObject("Wscript.Shell")
wshell.Run "C:\Users\txo4074\Documents\Projects\SAPscriptINVEST\myTest.vbs" & " " & "999Invest.xlsm" & " " & FileName
End Sub
我的测试.vbs
set xclapp = getObject(,"Excel.Application")
on error resume next
do
err.clear
Set xclwbk = xclApp.Workbooks.Item(wscript.arguments(1))
If Err.Number = 0 Then exit do
wscript.sleep 2000
loop
on error goto 0
xclapp.Run wscript.arguments(0) & "!ThisWorkbook.ExportInvest_1"
出口投资_1
Sub ExportInvest_1()
FileName = NewestFile("S:\FL_DMA\SAP Scripts\test\backup", "****")
Workbooks(Left(FileName, 25)).Worksheets(1).Activate
Range("A2:K100").Select
Selection.Copy ThisWorkbook.Worksheets("Input").Range("A2:K100")
Workbooks(Left(FileName, 25)).Close
ThisWorkbook.Activate
End Sub