cancel
Showing results for 
Search instead for 
Did you mean: 

How can I handle error messages from VBA?

Former Member
0 Kudos

Hello,

I've started using "Record script" and I've found it very useful, it's very powerful combined with VBA code.

By now, I'm executing it from Excel and I would love to know how can I handle SAP error messages.

For example:

Sub Test ()

'Some code

Dim material As String

material = "123456"

session.findById("wnd[0]/usr/ctxtRC29N-MATNR").Text = material

'Some code

End Sub

I wrote this routine to change 1000 materials information.

Everything went fine until the code tryed to modify a material that has some weird configuration and SAP returned an error message.

Thanks in advance,

Richard

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor
0 Kudos

Hello.

I have created an dynamic Excel VBA template which include as well this log-function.



Public Function Create_Log(lngRow As Long, lngCol As Long, strLog As String)

    Dim lngCounter As Long
    Dim lngLast_Col As Long
    Dim strLog_Line As String
   
    lngCounter = lngRow
    lngLast_Col = lngCol
    strLog_Line = strLog
   
    If ThisWorkbook.Sheets("SAP_PROCESS").Cells(lngCounter, lngLast_Col).Value = "" Then
        ThisWorkbook.Sheets("SAP_PROCESS").Cells(lngCounter, lngLast_Col).Value = strLog_Line
    Else
        ThisWorkbook.Sheets("SAP_PROCESS").Cells(lngCounter, lngLast_Col).Value = ThisWorkbook.Sheets("SAP_PROCESS").Cells(lngCounter, lngLast_Col).Text & Chr(10) & strLog_Line
    End If

End Function

In my process code I use this function like this:


'Log

If Session.FindById("wnd[0]/sbar").Text <> "" Then Create_Log lngCounter, lngLast_Col, Session.FindById("wnd[0]/sbar").Text

We can check as well if we receive an error (sy-msgty = 'E')


If Session.FindById("wnd[0]/sbar").messagetype = "E" Then Create_Log lngCounter, lngLast_Col, Session.FindById("wnd[0]/sbar").Text

Hope this give you an idea how to handle this.

Best regards,

Holger

Former Member
0 Kudos

Thanks Holger, I've tryed and it worked!

Answers (0)