I'm trying to automate some monthly manual sales orders in SAP GUI. What it does is opening 2 Excel-workbooks and import sales info to SAP GUI (SAP ERP transaction va01
).
The script works as follows:
- Open help-file, fetches address to Excel-files with sales info (in test I use 3 rows of addresses/files)
- Open sales info file with fetched address (in test I use 7 rows of data to copy, 8 rows in total including name row in each file)
- Imports sales info to SAP GUI (
va01
transaction), loops through each row. - Loops to next address in help-file and repeats until finished then close everything.
The problem is that not all info is copied to SAP, the last row is skipped for all but the last sales info file.
I figure the loop for rows in the sales info file skips the last row/closes the workbook to early when there is another sales info file to open, I’m very new to SAP GUI and VBS and can’t find a good way to let the loop finish adding info to SAP GUI before closing the currently open info file and then open the next one.
Thank you for any help/input to solve this
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Set xlapp = CreateObject("Excel.Application")
Dim xlhlpwb
Dim xlhlpsh
Dim xlwb
Dim xlsh
Dim Rw
'Adress to Help-file, open and select sheet 1
Set xlhlpwb = xlapp.Workbooks.Open("C:\tmp\help-file_test.xlsx")
Set xlhlpsh = xlhlpwb.Sheets(1)
'Loop for adresses to invoice data
for n = 1 to xlapp.ActiveCell.SpecialCells(11).Row
m = 1
wbadress = xlhlpsh.Cells(n,m).Value
'Open Workbook with invoice info and set worksheet to use, "Rw" is used in SAP to help insert the info
Set xlwb = xlapp.Workbooks.Open("" & wbadress & "")
Set xlsh = xlwb.Sheets(1)
Rw = 0
'Fetch info per row in worksheet
for i = 2 to xlapp.ActiveCell.SpecialCells(11).Row
for j = 1 to xlapp.ActiveCell.SpecialCells(11).Column
if j=22 then ARTICLE = xlsh.Cells(i,j).Value
if j=23 then AMOUNT = xlsh.Cells(i,j).Value
if j=25 then INFO = xlsh.Cells(i,j).Value
next
'Add info to SAP transaction (va01-"create invoice")
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,"& Rw &"]").text = ARTICLE
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtRV45A-KWMENG[2,"& Rw &"]").text = AMOUNT
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBKD-BSTKD[27,"& Rw &"]").text = INFO
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_POAN").press
'To make sure the right row in SAP is selected for next row in loop
Rw = 1
next
xlwb.Close True
Set xlsh = Nothing
Set xlwb = Nothing
next
Set xlwb = Nothing
Set xlsh = Nothing
xlhlpwb.Close True
Set xlhlpwb = Nothing
Set xlhlpsh = Nothing
xlapp.Quit
set xlapp = Nothing
Rw = 0