问题
When I run a SAP script in VBA (excel) over a long period of time, the SAP session starts to slow down. If I interrupt the code and start it again, the SAP session still runs at the slow speed it was at. If I close excel and run the code again, the SAP session still runs at the slow speed. However, if I close the SAP session and reopen it and then run the code (not closing excel), the speed goes back to optimal speed again.
I ran the below code (as a test) to see the speed decrease. When the code started, each loop took about 4 seconds, by the 300th loop however, the speed decreased to 16 seconds per loop.
This may seem trivial, but I am running another block of code (too big to post) which has about 3000 loops to perform, where each loop takes about 25 seconds to perform at optimal speed (a minute or longer per loop once SAP starts to slow down).
Rather than running a block of 100 loops, closing SAP, reopening SAP again and then running the next block of 100 loops (which is what I have been doing), does anybody know what slows down the SAP session and if there is a way to reset SAP programmatically so that the code returns to optimal speed again? Any help or direction would be greatly appreciated, I couldn’t find much on the internet regarding this.
Sub runScript()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim count As Integer
count = 1
Do While count < 1000
Dim startDateTime As Date
startDateTime = Now
Call enterExitTransactionIW33
Cells(count, 2) = DateDiff("S", startDateTime, Now)
count = count + 1
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
End Sub
Private Sub enterExitTransactionIW33()
Dim SAPGuiAuto, SAPApp, SAPConnection, SAPSession As Object
Set SAPGuiAuto = GetObject("SAPGUI")
Set SAPApp = SAPGuiAuto.GetScriptingEngine
Set SAPConnection = SAPApp.Children(0)
Set SAPSession = SAPConnection.Children(0)
SAPSession.findById("wnd[0]/tbar[0]/okcd").Text = "IW33"
SAPSession.findById("wnd[0]").sendVKey 0
SAPSession.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = "80808080"
SAPSession.findById("wnd[0]").sendVKey 0
SAPSession.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/tabsTS_1100/tabpVGUE").Select
SAPSession.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpMUEB").Select
SAPSession.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpKOAU").Select
SAPSession.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpPARU").Select
SAPSession.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpIOLU").Select
SAPSession.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpIHKD").Select
SAPSession.findById("wnd[0]/tbar[0]/okcd").Text = "/N"
SAPSession.findById("wnd[0]").sendVKey 0
End Sub
来源:https://stackoverflow.com/questions/54178988/sap-script-runs-slower-over-time-vba