Skip to Content
0
Sep 19, 2020 at 10:28 PM

Error handling in SAP scripting

400 Views

Hi,

I have a code to save invoices in pdf from TC VF03. I do it for a list of documents (listed in an Excel workbook).

I though have an issue with error handling. If for some reasons (billing doc. No. does not exist or certain doc. types cannot be saved) a specific item in the list cannot be saved then I would like the macro to write "error" in the workbook' specific field and jump on next item, without continuing the same loop (if it continued the naming of the pdfs would be screwed up).

Thank a lot in advance!

Here is my code:

Sub VF03_Save_Invoice()
Dim RowNum1 As Integer
RowNum1 = Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To RowNum1
If Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 32) = "Released" Then
Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
session.StartTransaction "VF03" 'Start the transaction
Dim filename As String
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = Workbooks("Billing Macro Daily").Worksheets("Invoices Issued").Cells(i, 1) 'specifying doc. No.
session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select 'choosing Billing Document - Issue output menu item
If ??? Then 'this is the statement I do not know, that is supposed to detect error
Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 33) = "Error"
Else
session.findById("wnd[1]/usr/tblSAPLVMSGTABCONTROL").getAbsoluteRow(0).Selected = True 'select first item
session.findById("wnd[1]/tbar[0]/btn[86]").press 'print
'Start asynchronous process that gives the file name and saves the file
filename = Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 1) & ".pdf"
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\Users\...\ScriptInvSave.vbs" & " " & filename
Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 33) = "Invoice saved"
End If

End If

Next i

End Sub