问题
I would have liked to have automated several extraction except that when I run the macro I have pop-up windows that annoy me terribly I can make them disappear by pressing enter.
Public Sub RunGUIScript()
Dim W_Ret As Boolean
Dim Société As String
Sheets("Extraction").Select
Société = Range("b9")
Application.SendKeys "{Enter}"
' Connect to SAP
W_Ret = Attach_Session
If Not W_Ret Then
Exit Sub
End If
On Error GoTo myerr
objSess.findById("wnd[0]").maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "S_ALR_87012039"
objSess.findById("wnd[0]/tbar[0]/btn[0]").press
objSess.findById("wnd[0]/usr/radSUMMB").Select
objSess.findById("wnd[0]/usr/chkP_GRID").Selected = True
[...]
Exit Sub
myerr:
MsgBox "Error occured while retrieving data", vbCritical + vbOKOnly
End Sub
The "Application.SendKeys "{Enter}" feature does not make my SAP windows disappear so it is not the solution that works
回答1:
The first popup is SAP telling you, that a script is trying to access SAP. You can disable that information in the SAP settings.
Go to Options -> Accessibility & Scripting -> Scripting
And then remove the checkmark from "Notify when a script attaches to SAP GUI" and "Notify when a script opens a connection"
The second popup can be closed with this line:
objSess.FindById("wnd[1]/tbar[0]/btn[0]").Press
This will press the Ok button in the popup.
And if you want to know if the window did popup you can do that like this:
On Error Resume Next
objSess.FindById("wnd[1]/tbar[0]/btn[0]").Press
If Err.Number = 0 Then
'Button was pressed
End If
On Error GoTo 0
Put this piece of code after the line of code that causes the window to popup.
来源:https://stackoverflow.com/questions/56749438/how-to-close-sap-pop-up-windows-with-vba