简体   繁体   中英

SAP Error handling using SAP GUI scripting and VBA

I am trying to automate some of my SAP tasks using VBA.

In case of SAP error, I want my VBA to show error as shown in error bar (in SAP).

If case of no error, proceed with Scripting code and end with message box "Process completed".

Please check my code. It is not working. Any suggestion or debugging would be highly appreciated.

col1 = Trim(CStr(objSheet.Cells(2, 1).Value))
col2 = Trim(CStr(objSheet.Cells(2, 2).Value))
col3 = Trim(CStr(objSheet.Cells(2, 3).Value))
  
'Scripting for VA03 data extraction

Session.FindById("wnd[0]").maximize

Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nxxxx"
Session.FindById("wnd[0]/tbar[0]/btn[0]").press

Session.FindById("wnd[0]/usr/ctxtVBAK-VBELN").Text = col1
Session.FindById("wnd[0]/usr/ctxtVBAK-VBELN").caretPosition = 10
Session.FindById("wnd[0]/tbar[0]/btn[0]").press

If Session.FindById("wnd[0]/sbar").Text = "SD Document" & "col1" & "is not in the database or has been archived" Then

  ' SAP GUI error processing
  objSheet.Cells(2, 3).Value = Session.FindById("wnd[0]/sbar").Text

Else

  Session.FindById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/" _
      & "ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/" _
      & "subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_PKON").press
  Session.FindById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\03").Select
  objSheet.Cells(2, 2).Value = Session.FindById( "wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/" _
      & "tabpT\03/ssubSUBSCREEN_BODY:SAPMV45A:4452/ctxtVBAP-VSTEL").Text

  MsgBox "Process Completed"

End If

You could check the SAP status bar like that

Function StatusBarError(Session as object) As Boolean

Dim objSapStatusBar As Object

   Set objSapStatusBar = Session.findById("wnd[0]/sbar")
   If objSapStatusBar.messagetype = "E" Then
       StatusBarError = True
   Else
       StatusBarError = False
   End If

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM