cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle a message box in VBScript

former_member586474
Discoverer
0 Kudos

Hi everyone,

i have a question when i am working with a VBA/VBScript to automate a process in SAP.

So to give overview:

I am performing several standardized changes for a contract in t-code VA42. Sometimes a message box appears which stops my script:

In these cases i just want the script to click "Save" and continue with the usual work.

Can you please help me with this one?

I already tried with Application.DisplayAllerts = False, but it does not work. I thought about Error Handler but this is basically not an error. Can you advise on a way to manage this?

I guess it can be done with an IF statement, but i do not know how to format my code.

Thank you in advance!

Code:

Sub Change()


today = Format(Date, "dd.mm.yyyy")
Application.DisplayAlerts = False


'We declared the variables for the while function in excel
Dim cont As String
Dim row As Integer
Dim rep As String


Dim j As Integer
j = 2


'Those are the commands with which we make SAP available for the VBA code
Set SapGuiAuto = GetObject("SAPGUI")
  Set SAPApp = SapGuiAuto.GetScriptingEngine
  Set SAPCon = SAPApp.Children(0)
  Set Session = SAPCon.Children(0)


  If IsObject(WScript) Then
   WScript.ConnectObject Session, "on"
   WScript.ConnectObject Application, "on"
 End If
 
Session.findById("wnd[0]").maximize
'Session.findById("wnd[0]/tbar[0]/btn[3]").press
'Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/okcd").Text = "va42"
Session.findById("wnd[0]").sendVKey 0




'We start the loop inside the macro book and give values to our variables
With ThisWorkbook
While Cells(j, 1) <> ""
cont = Cells(j, 1).Value
row = Cells(j, 3).Value
rep = Cells(j, 4).Value


' enter VBS code
'In this part we change the inst to REMV
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = cont
Session.findById("wnd[0]/usr/ctxtVBAK-VBELN").caretPosition = 8
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4426/subSUBSCREEN_TC:SAPMV45A:4908/tblSAPMV45ATCTRL_U_ERF_KONTRAKT").verticalScrollbar.Position = row
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4426/subSUBSCREEN_TC:SAPMV45A:4908/tblSAPMV45ATCTRL_U_ERF_KONTRAKT/ctxtVBAP-KDMAT[5,0]").SetFocus
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4426/subSUBSCREEN_TC:SAPMV45A:4908/tblSAPMV45ATCTRL_U_ERF_KONTRAKT/ctxtVBAP-KDMAT[5,0]").caretPosition = 6
Session.findById("wnd[0]").sendVKey 2


'change date
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\03").Select
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\03/ssubSUBSCREEN_BODY:SAPLV45W:4201/ctxtVEDA-VDEMDAT").Text = today
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\03/ssubSUBSCREEN_BODY:SAPLV45W:4201/ctxtVEDA-VDEMDAT").SetFocus
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\03/ssubSUBSCREEN_BODY:SAPLV45W:4201/ctxtVEDA-VDEMDAT").caretPosition = 10


'change INST to REMV
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\10").Select
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\10/ssubSUBSCREEN_BODY:SAPMV45A:4454/ctxtVBAP-KDMAT").Text = rep


'Deletes the first line in Technical Objects and saves the changes
Session.findById("wnd[0]/mbar/menu[3]/menu[9]").Select
Session.findById("wnd[0]/usr/tblSAPLIWOLOBJK_220").getAbsoluteRow(0).Selected = True
Session.findById("wnd[0]/tbar[1]/btn[19]").press
Session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/btn[11]").press


'we make sure the loop goes through every row and then end it
j = j + 1
Wend


End With
End Sub


Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor

Hello Mihail,

please disable at first in your SAP Logon options the using of the native Windows dialogs, like in the following image.

If you have done that and the dialog appears furthermore, instead of an SAP dialog, you can use AutoItX to handle dialogs easily. If you don't have the possibility to use AutoItX you could use the VBScript commands AppActivate and SendKeys, but this is a little bit tricky, you can find an example here.

Let us know your results.

Cheers
Stefan

Answers (0)