cancel
Showing results for 
Search instead for 
Did you mean: 

Code for scrolling to next line item in item tab for delivery date change in PO using VBA or Vbs?

Former Member
0 Kudos

Hi there,

I am trying to scroll to next line item in PO using ME22N , using VBA (currently), but facing some coding problem. If any of you can help me in this regard?

I have data in excel like PO #, Purchasing line item #, Material #, PO Qty,Delivery date & Statistical date.

Purchasing line item # is something like 20, 30, 40, but sometimes it could be 21, 32, 45.


So thing is that, code should identify Purchasing Line item # according to data in excel, scroll to the line item with specific #, than identify particular material with reference to Qty, and than change the required date from excel.


for reference i am attaching a snapshot of line item & the code on which i am working.



My code:


Sub Update_Delivery_statDate()

Dim App, Connection, session As Object, currentrow As Integer

Set SapGuiAuto = GetObject("SAPGUI")

Set App = SapGuiAuto.GetScriptingEngine

Set Connection = App.Children(0)

Set session = Connection.Children(0)

If IsObject(WScript) Then

   WScript.ConnectObject session, "on"

   WScript.ConnectObject Application, "on"

End If

Dim objExcel

Dim objSheet, intRow, i, j

Set objExcel = GetObject(, "Excel.Application")

Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 To objSheet.UsedRange.Rows.Count 'Assuming there is a header row

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

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nme22n"

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

Do Until objSheet.Cells(i, 2).Value = ""

If objSheet.Cells(i, 2).Value <> "" Then

COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1

COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2

COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3

COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) 'Column4

COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) 'Column5

COL6 = Trim(CStr(objSheet.Cells(i, 6).Value)) 'Column5

COL7 = Trim(CStr(objSheet.Cells(i, 7).Value)) 'Column5

COL8 = Trim(CStr(objSheet.Cells(i, 8).Value)) 'Column5

COL9 = Trim(CStr(objSheet.Cells(i, 9).Value)) 'Column5

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

session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").Text = COL2

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

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB1:SAPLMEGUI:6000/cmbDYN_6000-LIST").SetFocus

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB1:SAPLMEGUI:6000/btn%#AUTOTEXT002").press

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB1:SAPLMEGUI:6000/btn%#AUTOTEXT002").press

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB1:SAPLMEGUI:6000/btn%#AUTOTEXT002").press

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1320/tblSAPLMEGUITC_1320/ctxtMEPO1320-EEIND[2,0]").Text = COL9

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1320/tblSAPLMEGUITC_1320/ctxtMEPO1320-SLFDT[5,0]").Text = COL9

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1320/tblSAPLMEGUITC_1320/ctxtMEPO1320-SLFDT[5,0]").SetFocus

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1320/tblSAPLMEGUITC_1320/ctxtMEPO1320-SLFDT[5,0]").caretPosition = 10

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

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

session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press

session.findById("wnd[0]/sbar").DoubleClick

session.findById("wnd[0]/shellcont").Close

objSheet.Cells(i, 3).Value = "DONE"

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

On Error Resume Next

End If

i = i + 1

Loop

Set objSheet = Nothing

Set objExcel = Nothing

Set SapGuiAuto = Nothing

Exit For

Next

MsgBox "Process Completed"

End Sub

0 Kudos

how do change the delivery date in PO using VBA

Scrolling down problem

jerryjanda
Community Manager
Community Manager
0 Kudos

Hi, revanthbalan:

While we're happy that you've come to SAP Community to get an answer to your question, you posted your question as an answer in a thread from more than 7 years ago -- filled with Former Members who won't even get a notification that you've replied here.

I've converted your answer to a comment, but even so -- posting in older threads is not the best way to get guidance.

If you're looking for help, you should ask a new question: https://answers.sap.com/questions/ask.html.

Here are some tips to help you craft an effective question for our community: https://community.sap.com/resources/questions-and-answers, https://developers.sap.com/tutorials/community-qa.html, https://groups.community.sap.com/t5/welcome-corner-discussions/advice-from-sap-champions-questions-a....

I encourage you to follow this guidance, as I'd really like to see you get a solution to your problem.

I hope you find this advice useful!

All the best,

--Jerry

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

assuming your problem is locating the correct line item, I would loop through the item overview section, looking for the correct item nbr, then choose the item nbr to get the detail section to change to that item nbr.

Here is an example of performing just those steps

Sub ME22N_EditPOLineItemTest()

DesiredItemNbr = "20"

NewDeliveryDate = "09/01/2015"

For x = 1 To 100

  'Compare Item Nbr in first displayed row of item overview section to desired item nbr

  If DesiredItemNbr = SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-EBELP[1,0]").Text Then

    FoundMatch = True

    'select the Item nbr field and press F2 to get the item detail section to change

    SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-EBELP[1,0]").SetFocus

   'This is the Choose PO Item (Press F2) step

   SAPSession.FindById("wnd[0]").SendVKey 2

    Exit For

  Else

    'scroll item section down down one more row (so we are always comparing to the top visible row)

    SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = x

  End If

Next x

'if we found the item nbr, then go to work in the item detail section

If FoundMatch Then

  'edit dleivery date field

  SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1320/tblSAPLMEGUITC_1320/ctxtMEPO1320-EEIND[2,0]").Text = NewDeliveryDate

End If

End Sub

Former Member
0 Kudos

Dear Chad,

Thank you, it worked.

Now my major task is to change the date of material if there are 2 rows & 2 different delivery dates of same material based on Qty required column.

I will try to post it visually , if i find it. If you got the concept please suggest.

Former Member
0 Kudos

Dear Chad,


If DesiredItemNbr =

SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-EBELP[1,0]").Text



The above HIGHLIGHTED value changes every time with change in the line item no.  while coding and it shows error.


How to control that value???

Former Member
0 Kudos

it looks like in ME22N, the item overview screen number flips between 0015 and 0019 depending on which item detail tab is active.  There is probably a more elegant way to do this, but since there are only 2 we have to check for it's not much code to just check if one exists, if not use the other.

'when Delivery Schedule item detail tab is active, item overview subscreen = 0015, otherwise it might be

'0015 or it might be 0019.  check to see which one exists, then click on Delivery schedule tab

'use the FindById method with the optional second parameter set to False to avoid an error and set it to a new object (sapobj) then see if that object actually got set.

Set sapobj = SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5", False)

If Not sapobj Is Nothing Then

  SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

Else

SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

End If

'now you can continue on using 0015...

For x = 1 To 100

  'SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").getAbsoluteRow(0).Selected = True

  'Compare Item Nbr in first displayed row of item overview section to desired item nbr

  If DesiredItemNbr = SAPSession.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-EBELP[1,0]").Text Then

    FoundMatch = True

...

...

...

Former Member
0 Kudos

Chad,

I've tried with the above method.

But the thing is that, the value i.e 0015, varies from something between 0010,0015,0019,0020 or something else in my notice.

as I've used your code it worked for one time SAP login for first value 0010 &0015, and if logged in again that first value changed to 0020.

Set sapobj = session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5", False)

If Not sapobj Is Nothing Then

  session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

Else

session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

End If

With Fresh SAP login:

session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0020/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").getAbsoluteRow(0).selected = true

And in between the code SAP is taking time to respond and crashes too.

So , if you could help me to control this would be of great help.

Former Member
0 Kudos

So now the clunkiness of this approach stars to show, but it should still work to get the detail tab selected, then it will be subscreen 0015 after that.  I cannot get my system to flip between anything other than 0015 and 0019, so I can't thoroughly test it.  Let me know if it works.

'try 0010

Set sapobj = session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5", False)

If Not sapobj Is Nothing Then

  session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

Else 'try 0015

  Set sapobj = session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5", False)

  If Not sapobj Is Nothing Then

     session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0015/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

  Else  'try 0019

     Set sapobj = session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5", False)

     If Not sapobj Is Nothing Then

     session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

    Else  'try 0020

       Set sapobj = session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0020/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5", False)

        If Not sapobj Is Nothing Then

           session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0020/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT5").Select

        End If

    End If

  End If

End If

Former Member
0 Kudos

Chad,

For now its working , but there is some problem regarding SAP response . It even stops working in between or crashes some time only when code is running. Any specific reason ??? Or if you can suggest something??

One more, thing!!

As you said there is more elegant way to do this process.If so, please advise or share your views and method!!!

Thanks

Former Member
0 Kudos

I would need to see where it crashes, it might need some pauses or it might be something else

Former Member
0 Kudos

Chad,

This is the error which i get when SAP crashes=

- Run time error'-2147023170 (800706be)':

Automation error

The remote procedure call failed.





It crashes some where between:

For x = 1 To 100

  'Compare Item Nbr in first displayed row of item overview section to desired item nbr

If DesiredItemNbr = session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-EBELP[1,0]").Text Then

    FoundMatch = True

    'select the Item nbr field and press F2 to get the item detail section to change

    session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-EBELP[1,0]").SetFocus

   'This is the Choose PO Item (Press F2) step

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

    Exit For

  Else

    'scroll item section down down one more row (so we are always comparing to the top visible row)

    session.FindById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.Position = x

  End If

Next x

And some time it crashes without showing any error.

Please Advise.

Former Member
0 Kudos

Chad,

Please Respond.

Answers (1)

Answers (1)

holger_khn
Contributor
0 Kudos

Hello.

As this can be tricky I suggest to use filter Option, then required item will be on first Position in above table. Then we just Need to press 'Choose PO item' button. Then you will be on Detail Tabs for PO item.