Skip to Content
avatar image
Former Member

VA02 Sales Order Automation?

Hello All,

I am very new to this vbs.. so need you guidance and support to solve my query. I am trying to automate Sales order line items details updation and i have recorded the below script and than modified a bit e.g. added excel codes which take the values from the excel. Here is the overview of the script, First goes to transaction VA02 and then take the sales order number from the excel sheet and enter the sales order and go to Item Overview tab and updates the first line description and than page down button is pressed to go to the last empty line where details are update like Material code, Qty. Descrption, WBSe element, etc these are updated from excel sheet and then the required details are taken from the excel sheet and updated in the sales order and SO is then saved.

The script runs fine, but only when i update the SO which was used for recording, but if the SO is changed it gives me error due to the Line selected for updation of material, Description, Qty and WBSe... If you check below the item overview ROW is 6 for the recorded SO which is hard coded, so how this can be solved of removing the hard code coding to a variable one which depends on the SO row number.

MABNR[1,6]"

KWMENG[3,6]")

UEPOS[2,6]")

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 xclapp = CreateObject("Excel.Application")

Set xclapp = CreateObject("Excel.Application")

Set xclwbk = xclapp.Workbooks.Open("c:\va02.xlsx")

set xclsht = xclwbk.Sheets("Sheet1")

For i = 2 to 8

While (not xclsht.Cells(i, 1).Value = "")

session.findById("wnd[0]").maximize

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/tbar[0]/okcd").text = "va02"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtVBAK-VBELN").text = xclsht.Cells(i,1).Value

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02").select

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,0]").text = xclsht.Cells(i,2).Value

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,0]").setFocus

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,0]").caretPosition = 0

session.findById("wnd[0]/tbar[0]/btn[83]").press

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,6]").text = "XXXXXX"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtRV45A-KWMENG[3,6]").text = "1"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtVBAP-VRKME[4,6]").text = "EA"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,6]").text = xclsht.Cells(i,3).Value

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtVBAP-PSTYV[7,6]").text = "ZP13"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-UEPOS[2,6]").text = "20"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtVBAP-WERKS[8,6]").text = "85MP"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtVBAP-PS_PSP_PNR[9,6]").text = xclsht.Cells(i,4).Value

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,6]").setFocus

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,6]").caretPosition = 10

session.findById("wnd[0]").sendVKey 2

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,6]").setFocus

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtVBAP-ARKTX[6,6]").caretPosition = 12

session.findById("wnd[0]").sendVKey 2

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\07").select

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\07/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN").verticalScrollbar.position = 11

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\07/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/ctxtKOMV-KSCHL[1,9]").text = "zpvp"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\07/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KBETR[3,9]").text = xclsht.Cells(i,5).Value

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\07/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KBETR[3,9]").setFocus

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\07/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KBETR[3,9]").caretPosition = 16

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\06").select

session.findById("wnd[1]").close

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-AFDAT[0,0]").text = xclsht.Cells(i,6).Value

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-FAREG[9,0]").text = "3"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-FAREG[9,0]").setFocus

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-FAREG[9,0]").caretPosition = 1

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-FAKSP[7,0]").text = ""

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-FAKSP[7,0]").setFocus

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/tblSAPLV60FTCTRL_FPLAN_TEILFA/ctxtFPLT-FAKSP[7,0]").caretPosition = 0

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\16").select

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4462/subKUNDEN-SUBSCREEN_8459:SAPMV45A:8459/ctxtVBAP-ZZSMC").text = "o"

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4462/subKUNDEN-SUBSCREEN_8459:SAPMV45A:8459/ctxtVBAP-ZZSMC").caretPosition = 1

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 32

session.findById("wnd[0]/tbar[0]/btn[3]").press

i = i + 1

Wend

Next

xclwbk.save

msgbox "Document Saved"

Set xclwbk = Nothing

Set xclsht = Nothing

xclapp.Quit

set xclapp = Nothing

Thanks for all your help in advance.

Sushil

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 13, 2013 at 11:32 AM

    Hello Sushil,

    I'm not sure whether this is going to work, but can you try:

    ROW = session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG").currentRow

    session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1," & ROW & "]").text = "XXXXXX"

    Thanks,

    Sayuti

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 18, 2013 at 06:14 PM

    Hi Sayuti....

    The code is working just perfect... Thanks very Much... 😊

    Can you plz help with the error handlers too...

    For some SO a additional message pops up for Ex - Credit Check block. Below are some areas...

    1) session.findById("wnd[0]/tbar[0]/okcd").text = "va02"

    session.findById("wnd[0]").sendVKey 0

    After this for some SO's a Credit block message appears.

    So if this comes i want session.findById("wnd[0]").sendVKey 0 this code to trigger.

    2) session.findById("wnd[0]").sendVKey 32

    After saving the SO a credit block message appears

    So if this comes i want session.findById("wnd[0]").sendVKey 0 this code to trigger.

    3) session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\06").select

    session.findById("wnd[1]").close

    After updating the amount in the condition tab and when we go to the billing plan tab sometimes the Billing box is ticked which is to be removed and then hit enter below is the code.

    session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/chkRV60F-KFREL").selected = false

    session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\06/ssubSUBSCREEN_BODY:SAPLV60F:4203/chkRV60F-KFREL").setFocus

    session.findById("wnd[0]").sendVKey 0

    So if the billing is ticked it should run the above code.

    4) For Some SO a error messages(Material code) is highlighted at the bottom of the SAP screen when save button is pressed to save the SO, so enter key are to be press( For example if SO contains 10 line items then enter key are to be pressed ten times or in other words the enter key are to be pressed till the SO is saved and SAP is on the VA02 main screen )

    Thanks for your help in advance.....

    Sushil

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Sayuti Azmi

      Hello Sayuti,

      I discovered SAP's ability to automate using vbscript a few weeks ago and have been working diligently (10hrs per day) trying to automate the VA01 sales order process.

      Reading through all the SAP forums Online has been extremely helpful and I'm almost at the finish line. The only remaining part where I'm completely lost is the scroll bar (GuiTableControl)

      I read through yours and Stefan's examples on this topic and tried every combination over and over and still cannot place more than 16 rows of data in the SAP table before it crashes and we sometimes receive sales orders that require up to 99 rows!

      (16 rows are what is visible before moving the scroll bar.) Am I missing a step from your instructions? Do I need to declare an Object that activates the scrollbar?

      I should also note that the scroll bar doesn't even appear until after at least one line item and quantity is placed on a row and you press enter. Is there a way around this? Ideally I would love to just open SAP and have the Scrollbar available already.

      I understand that you've already covered this topic a few years ago but please help me...I'm sleep deprived and stressed out.

      The photo above is the table I'm using. Notice: there is no scroll bar and there are 16 visible rows.

      Below is my code. In my example I'm trying to input 30 line items and quantities from an excel spreadsheet. I used your example from the thread "VA02 Sales Order Automation" but it does not work for me. I should also note TotalRow and VisibleRow both = 16 So my BlankRow is always = 0. Thanks a TON!!!


      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

      Dim SapGuiApp, Connection, Session, FileObject, oFile, Counter
      Dim ApplicationPath, CredentialsPath, ServerPath, FilePath
      Dim ExcelApp, ExcelWorkbook, ExcelSheet
      Dim sRow, eRow, J
      Dim BlankRow, TotalRow, VisibleRow, Scroll
      Set ExcelApp = CreateObject("Excel.Application")
      Set ExcelWorkbook = ExcelApp.Workbooks.Open("C:\Personal\02039116\Desktop\SAP Automation\TestOrder00.xlsx")
      Set ExcelSheet = ExcelWorkbook.Worksheets(1)

      eRow = 1

      session.findById("wnd[0]").maximize
      session.findById("wnd[0]/tbar[0]/okcd").text = "VA01"
      session.findById("wnd[0]").sendVKey 0
      session.findById("wnd[0]/usr/ctxtVBAK-AUART").text = "ZWO"
      session.findById("wnd[0]/usr/ctxtVBAK-VTWEG").text = "10"
      session.findById("wnd[0]/usr/ctxtVBAK-VTWEG").setFocus
      session.findById("wnd[0]/usr/ctxtVBAK-VTWEG").caretPosition = 2
      session.findById("wnd[0]").sendVKey 0

      session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/txtVBKD-BSTKD").text = ExcelSheet.Range("F5").value
      session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR").text = ExcelSheet.Range("F4").value
      session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/ctxtRV45A-KETDAT").text = ExcelSheet.Range("F6").value
      session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/ctxtRV45A-DWERK").text = ExcelSheet.Range("F7").value

      session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG").VerticalScrollbar.position = 0
      TotalRow = session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG").RowCount
      VisibleRow = session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG").VisibleRowCount

      BlankRow = TotalRow - VisibleRow

      If BlankRow > VisibleRow Then

      Scroll = BlankRow - VisibleRow + 1
      sRow = VisibleRow - 1

      Else

      Scroll = 0
      sRow = BlankRow
      End If

      For J = 1 To 30

      session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG").VerticalScrollbar.position = Scroll
      session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,"& sRow & "]").text = ExcelSheet.Cells(eRow, 2)
      session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\02/ssubSUBSCREEN_BODY:SAPMV45A:4401/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtRV45A-KWMENG[2,"& sRow & "]").text = ExcelSheet.Cells(eRow, 1)

      eRow = eRow + 1
      sRow = sRow + 1
      Next

      VA01.jpg (130.6 kB)