VB脚本调用VBA脚本后SAP端无响应,疑与SAP GUI及Office365升级有关
Hey there, let's break down what might be causing your SAP VBS script to hang after calling the Excel macro, especially since you mentioned recent SAP GUI and Office 365 upgrades. Here are the most likely culprits and fixes to try:
1. Unreliable Excel process handling via WScript.Shell
Your current code uses wshell.run with the true flag to wait for Excel to finish, but Office 365 upgrades can introduce background processes or delayed shutdown behavior that makes this wait mechanism unreliable. Instead of relying on the shell, control Excel directly via its object model for full visibility:
' Replace the wshell.run line with this code Dim excelApp, targetWorkbook Set excelApp = CreateObject("Excel.Application") excelApp.Visible = True ' Enable for debugging, can set to False later ' Open the workbook and run your macro Set targetWorkbook = excelApp.Workbooks.Open("\\denbppfs002\Abteilungslaufwerke\Produktion\Produktionsreporting\Makros\Autostart OpenProcessOrders.xlsm") excelApp.Run "Autostart OpenProcessOrders.xlsm!YourMacroName" ' Replace with your actual macro name ' Clean up explicitly targetWorkbook.Close SaveChanges:=True excelApp.Quit Set targetWorkbook = Nothing Set excelApp = Nothing
This ensures Excel is properly closed after the macro runs, eliminating the chance of lingering processes that keep your SAP script waiting.
2. SAP session closing loop is failing silently
Your while connection.children.count > 0 loop uses On Error Resume Next, which hides errors that might be preventing sessions from closing. After SAP GUI upgrades, session close behavior or element IDs could have changed, and the error suppression means you can't see where it's breaking.
Modify the loop to add timeout protection and remove blind error suppression (at least for debugging):
Dim timeoutStart, maxTimeoutSeconds timeoutStart = Now maxTimeoutSeconds = 15 ' Adjust based on your typical session close time Do While connection.Children.Count > 0 And DateDiff("s", timeoutStart, Now) < maxTimeoutSeconds Set asession = connection.Children(0) On Error Resume Next asession.findById("wnd[0]").close WScript.Sleep 500 ' Wait for confirmation popup asession.findById("wnd[1]/usr/btnSPOP-OPTION1").press On Error GoTo 0 ' Reset error handling WScript.Sleep 500 ' Give SAP time to process the close Loop ' Add a check for leftover sessions If connection.Children.Count > 0 Then MsgBox "Warning: Failed to close all SAP sessions within timeout." End If
Temporarily removing On Error Resume Next entirely will let you see exact error messages (like missing element IDs) that are breaking the session close.
3. Office 365 macro security changes blocking execution
Office 365 upgrades often tighten macro security, which can cause your Excel macro to hang or fail silently:
- Check trusted locations: Add the shared folder
\\denbppfs002\Abteilungslaufwerke\Produktion\Produktionsreporting\Makrosto Excel's trusted locations (File > Options > Trust Center > Trust Center Settings > Trusted Locations). This prevents Excel from blocking the macro. - Add error handling to your Excel VBA: If the macro hits an unhandled error, Excel will hang with a dialog, which keeps your SAP script waiting. Add this to your macro:
Sub YourMacroName() On Error GoTo MacroErrorHandler ' Your existing macro code here Exit Sub MacroErrorHandler: MsgBox "Macro failed: " & Err.Description, vbCritical ThisWorkbook.Close SaveChanges:=True Application.Quit End Sub
4. Lingering background processes
Upgrades can leave orphaned Excel or SAP GUI processes running, which interfere with subsequent script runs. Add this to the start of your SAP script to clean up leftover Excel processes (note: this closes all Excel windows, so save any open files first):
' Kill lingering Excel processes Dim wmiService, excelProcesses Set wmiService = GetObject("winmgmts:\\.\root\cimv2") Set excelProcesses = wmiService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'EXCEL.EXE'") For Each process In excelProcesses process.Terminate() Next Set excelProcesses = Nothing Set wmiService = Nothing
Start with debugging the SAP session close loop (remove error suppression) and verifying your Excel macro can run without hanging—these are the most common issues after version upgrades.
内容的提问来源于stack exchange,提问作者Lutz Fricke

