(Solved) – Automating the Download of Billing Documents from transaction VF03 in SAP

(solved)-–-automating-the-download-of-billing-documents-from-transaction-vf03-in-sap

I am trying to create a Macro to download multiple invoices from a SAP Transaction. However, it is still not working properly and I get error 614 and 814.
Please see the following steps of how we are currently doing it:

  1. Open SAP.
  2. Look for VF03 transaction.
  3. Open VF03 transaction.
  4. Enter the document number.
  5. Click on “Billing document”. (This will display a drop-down menu)
  6. Click on “Issue output to”. (From the drop-down menu)
  7. From the new sub window that displays, select the document.
  8. Click “Print preview”.
  9. Another window will display. Click “Print preview” on this window. (This opens the document)
  10. Print it out.
  11. Choose the folder in which you want to save it.
  12. Name the file. (It is standard to name it with the document number).

I want to have this as an automate process in which the following happens:

  1. Enter the list of invoices in column A of the Excel sheet.
  2. VBA connects with SAP and completes the process I explained before.
  3. The invoices go to a specific folder and are saved under its document number.

I have the following code:


Sub Downloadinvoices()


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 Connection = SAPApp.Children(0) 'Get the first session (window) on that connection



Dim i As Byte
Dim lastcell As Byte



lastcell = WorksheetFunction.CountA(Range("A:A"))



On Error GoTo errorhandler
For i = 2 To lastcell



Dim docnr As String
docnr = Cells(i, 1)



'1. Open transaction VF03
session.findById("wnd0tbar0okcd").Text = "NVF03"
'2. Press Enter
session.findById("wnd0").sendVKey 0
'3. Type Document number
session.findById("wnd0usrtxtRF05L-BELNR").Text = docnr
'4. Type Company Code
session.findById("wnd0usrctxtRF05L-BUKRS").Text = Range("b2").Value
'5. Type Fiscal Year
session.findById("wnd0usrtxtRF05L-GJAHR").Text = Range("c2").Value
'6. Press Enter
'session.findById("wnd0usrtxtRF05L-GJAHR").caretPosition = 4
session.findById("wnd0tbar0btn0").press
'7. Open the Attachment window
session.findById("wnd0titlshellcontshell").pressContextButton "GOS_TOOLBOX"
session.findById("wnd0titlshellcontshell").selectContextMenuItem "GOS_VIEW_ATTA"



session.findById("wnd1usrcntlCONTAINER_0100shellcontshell").currentCellColumn = "BITM_DESCR"
'9. Select the first row in attachment list
session.findById("wnd1usrcntlCONTAINER_0100shellcontshell").selectedRows = "0"
'10. Double click in the row to open the attachment
session.findById("wnd1usrcntlCONTAINER_0100shellcontshell").doubleClickCurrentCell



application.Wait (Now()   TimeValue("00:00:04")) '11. Wait 4 seconds
application.SendKeys "left" '12. Move to left
application.Wait (Now()   TimeValue("00:00:01")) '13. Wait 1 second
application.SendKeys "ENTER" '14. Press enter
application.Wait (Now()   TimeValue("00:00:01")) '15. Wait 1 second



session.findById("wnd1usrsub:SAPLSPO4:0300ctxtSVALD-VALUE0,21").Text = "C:Usersisaac.chavarriaDesktop"
'16. Choose the route to save
application.SendKeys Range("f2").Value Range("b2").Value "_" docnr



application.Wait (Now()   TimeValue("00:00:01")) '17. Wait a second
application.SendKeys "ENTER" '18. Press Enter
application.Wait (Now()   TimeValue("00:00:01")) '19. Wait a second
application.SendKeys "ENTER" '20. Press enter
application.Wait (Now()   TimeValue("00:00:01")) '21. Wait a second
application.SendKeys "F12" '22. Press F12 (back)
application.Wait (Now()   TimeValue("00:00:01")) '23. Wait a second
application.SendKeys "ENTER" '24. Press Enter
application.Wait (Now()   TimeValue("00:00:01")) '25. Wait a second
application.SendKeys "F12" '26. Press F12
application.Wait (Now()   TimeValue("00:00:01")) '27. Wait a second
application.SendKeys "F12" '28. Press F12
application.Wait (Now()   TimeValue("00:00:01")) '29. Wait a second
application.SendKeys "F12" '30. Press F12



errorhandler:
If Err.Number 0 Then
Cells(i, 1).Interior.Color = RGB(255, 0, 0)
On Error GoTo -1
End If



Next



MsgBox "Invoices already downloaded! (except any cells in red)", , "Isaac Chavarria says:"



End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *