-3

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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
ritu raj
  • 1
  • 1
  • 1
    I am not sure I understand where the problem is exactly (probably because I know nothing about SAP). Which line is not working for you? What is exactly not working? In your `If` condition you have `"SD Document" & "col1" & "is not in the database or has been archived"`. Do you actually mean `...& col1 &...`? Also check your spaces. If `col1 = 5`, the expression (after removing the double-quotes as I suggested) will result in `"...Document5is not ..."` – Super Symmetry Jul 19 '20 at 15:35
  • 1
    What "is not working"? What were your findings during your debugging? Please read [how to ask a good question](https://stackoverflow.com/help/how-to-ask). – Sandra Rossi Jul 20 '20 at 15:30

1 Answers1

1

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
Storax
  • 11,158
  • 3
  • 16
  • 33