Search code examples
excelvbavbscriptsap-gui

VBScript, click save to continue Script


I have a problem with a VBScript when I try to automate a procedure with VBA/VBS macro and SAP GUI Scripting.

It enters a contract number in VBS and does some standardized changes in SAP ERP transaction code VA03, but for some there are some incomplete information, then this pop-up window appears:

enter image description here

I only want my script to click Save and continue in case this happens for a certain contract number.

It did not work with Applications.DisplayAlerts = False, and also I guess it will not work with error handler as practically this is not an error.

It should be with a simple IF statement, but I do not know how I should put the wording.

Can anyone please help, my research in the net got me nowhere :(

Code (although it is working, I only need a piece which will handle the above stopper):

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/ctxtVBAP-KDMAT[5," & CStr(row) & "]").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," & CStr(row) & "]").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

Best regards, Mihail


Solution

  • First, I would record the missing piece of vbs program. Then you have to recognize where in the main program this piece must be installed. But I suspect it will happen after saving. The rough structure might look like this:

    . . .
    Session.findById("wnd[0]/tbar[0]/btn[11]").press
    '------------------------------ new ---------------------------
    on error resume next
    'Suppose that's the missing piece of program
    session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
    on error goto 0
    '------------------------------ new ---------------------------
    'we make sure the loop goes through every row and then end it
    j = j + 1
    Wend
    . . .
    

    Regards, ScriptMan