Skip to Content
0
Jan 27, 2021 at 08:32 PM

Script to attach documents in VA02 does not run

195 Views

Hi guys, good evening.

Using some previous answers I built a script that aims to add attachments (emails) to orders via VA02 in the DEP Production environment. My issue is that the script gets the Run-time error 619, and since I am quite new in VBA I am struggling finding the root cause of the issue.

I have two columns in my Excel file. One for the order number, and the other one for what should be the name with which the file should be saved. Maybe I should actually place the path where the file is stored?

Hope someone can help me. This is my line of code:

Sub AddAttachments()
Dim UpdateTo As String
Dim cell As String
Dim i As Long
Dim maxi As String
maxi = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
i = 3
UpdateTo = Range("B" & i)
cell = Range("A" & i)
Do While i < maxi + 1
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)
session.findById("wnd[0]").maximize 'opens SAP Window
session.findById("wnd[0]/tbar[0]/okcd").Text = "/NVA02" 'Writes the name of the transaction to work on, in this case VA02
session.findById("wnd[0]").sendVKey 0 ' Push Enter
session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = cell 'Writes down the number of the order to edit
session.findById("wnd[0]").sendVKey 0 ' Push Enter
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
session.findById("wnd[0]").sendVKey 11
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "G:\Finance\CCD\CCD Common\IOI-RU-GTME\LAC CUSTOMER SUPPLY\Team\Macros\Editor\Alamo Group - January Orders.msg"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = UpdateTo
session.findById("wnd[1]/tbar[0]/btn[0]").press
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Credit check (maximum % for open items exceeded pop-up
End If
If session.ActiveWindow.Name = "wnd[1]" Then
    session.findById("wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1").press
End If
Range("C" & i) = "Attachment added."
i = i + 1
Loop
MsgBox "All the attachments have been added."
End Sub