cancel
Showing results for 
Search instead for 
Did you mean: 

Re: Error handling in SAP scripting

09-21-2020 8:33 AM
script_man Active Contributor
2569 views 3 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

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
0 Likes
View Entire Topic
script_man
Active Contributor

Hi Agnes,

as always, there are definitely several ways to solve it. One of them could look like this:

Sub VF03_Save_Invoice()
Dim RowNum1 As Integer
Dim filename As String

RowNum1 = Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(Rows.Count, "A").End(xlUp).Row

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

For i = 2 To RowNum1
If Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 32) = "Released" Then
session.StartTransaction "VF03" 'Start the transaction
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = Workbooks("Billing Macro Daily").Worksheets("Invoices Issued").Cells(i, 1) 'specifying doc. No.
on error resume next
session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select 'choosing Billing Document - Issue output menu item

If err.number <> 0 then
on error goto 0
Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 33) = "Error"
Else
on error goto 0
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

Regards, ScriptMan

kezsmarkia
Explorer
0 Likes

Dear ScriptMan,

I appreciate your help with the issue!

When I run the code reaching a number that does not exist in VF03 "If Err.Number <> 0 Then" does not identify, recognize that there is an error and jumps to "Else" instead of error handling (writing Error into the workbook).

Could you please advise?

Many thanks and best regards, Ágnes

kezsmarkia
Explorer
0 Likes

I rearranged a bit and now it works! Dear ScriptMan, I appreciate your help, thank you!!!

Just had to bring one command up, because the error seemed to pop up there.

Sub D_Apcis_VF03_Save_Invoice()




Dim RowNum1 As Integer
Dim filename As String


RowNum1 = Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(Rows.Count, "A").End(xlUp).Row


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


For i = 2 To RowNum1


If Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 32) = "Released" Then
session.StartTransaction "VF03" 'Start the transaction
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 1)
On Error Resume Next
session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select
session.findById("wnd[1]/usr/tblSAPLVMSGTABCONTROL").getAbsoluteRow(0).Selected = True 'select first item
If Err.Number <> 0 Then
On Error GoTo 0
Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 33) = "Error"
Else
On Error GoTo 0
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:\....vbs" & " " & filename
Workbooks("Billing Macro Daily.xlsb").Worksheets("Invoices Issued").Cells(i, 33) = "Invoice saved"
End If
End If
Next i

End Sub



script_man
Active Contributor
0 Likes

Hi Agnes,

as you can see, I haven't been able to test it. But you have found the right solution yourself.

Regards, ScriptMan