Arizonaer
Arizonaer

Reputation: 1

VBA Excel to SAP Sendkeys

Good day all,

Please excuse me as I am new to scripts and trying to use VBA to SAP. Currently at my job we are using SAP and I am trying to use a script to automate a procedure that is manually time consuming. I recorded the script but I am using Excel via VBS to process an entire list instead of manually changing records as doing roughly 100 a day is time consuming. When I recorded the script I get to a point that I can not figure out because depending of each record the lines change: example is in record 1 it is 13, Column2 and record 2 it would be 10, Column2 and record 3 it might be 20, Column2. This is in column 2, workitem id always What do these rows have in common? The screen shot below is showing the page I get stuck at and if you can see the number 689 is highlighted. I need to click on the number to the left of 689 and as the examples above said.

I used find to search for 689 and it highlights the line BUT the cursor is actually in the Find box in the left hand corner beside the green check. I cannot figure out a way to make it move to the workitem id (left of the 689 number). I have tried a few things to include trying to open different branches to find a common location of the workitem, I tried sendVKey, sendkeys {TAB} and no luck some far. I just cannot get past this point.

Here is the script I have so far:

Sub AROtoARO()
' Ensure the script is run from the "AROtoARO" sheet
    If ActiveSheet.Name <> "AROtoARO" Then
    MsgBox "This script can only be run from the 'AROtoARO' sheet."
    Exit Sub
    End If
    Dim SapGuiAuto As Object
    Dim application As Object
    Dim connection As Object
    Dim session As Object
' Initialize SAP GUI scripting objects
    On Error Resume Next
    Set SapGuiAuto = GetObject("SAPGUI")
    If Err.Number <> 0 Then
    MsgBox "Could not get SAPGUI object"
    Exit Sub
    End If
    On Error GoTo 0
    Set application = SapGuiAuto.GetScriptingEngine
    If Err.Number <> 0 Then
    MsgBox "ERROR ERROR...Could not get scripting engine, are you sure this thing is on??"
    Exit Sub
    End If
    On Error GoTo 0
    Set connection = application.Children(0)
    Set session = connection.Children(0) 'SAP window PR1(1)
' Loop through each row starting from row 2
    Dim lastRow As Long
    Dim i As Long
    lastRow = ThisWorkbook.Sheets("AROtoARO").Cells(ThisWorkbook.Sheets("AROtoARO").Rows.Count,     1).End(xlUp).Row
    For i = 2 To lastRow
    Dim MIPRNumber As String
    MIPRNumber = ThisWorkbook.Sheets("AROtoARO").Cells(i, 1).Value

' Execute the SAP GUI script
    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/n"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/tbar[0]/okcd").Text = "SWi1"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/usr/ctxtCD-LOW").Text = ThisWorkbook.Sheets("AROtoARO").Cells(i,     3).Value
' Excel.application.Range("C2").Value
    session.findById("wnd[0]/usr/ctxtCT-LOW").Text = "00:00:01"
    session.findById("wnd[0]/usr/ctxtCT-LOW").SetFocus
    session.findById("wnd[0]/usr/ctxtCT-LOW").caretPosition = 8
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, "WI_TEXT"
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "WI_TEXT"
    session.findById("wnd[0]/tbar[1]/btn[38]").press
    session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text =           
    ThisWorkbook.Sheets("AROtoARO").Cells(i, 1).Value 'mipr number on excel
    number'session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/
    ctxt%%DYN001-LOW").caretPosition = 15
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellColumn = "WI_TEXT"
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").doubleClickCurrentCell
    session.findById("wnd[0]/tbar[1]/btn[35]").press
    session.findById("wnd[0]/tbar[1]/btn[21]").press
    session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").selectItem " 1", "Column2"
    session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell")
    .ensureVisibleHorizontalItem " 1", "Column2"
    session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").clickLink " 1", "Column2"
    session.findById("wnd[0]/mbar/menu[2]/menu[9]").Select
    session.findById("wnd[0]/mbar/menu[1]/menu[0]").Select
    session.findById("wnd[0]/shellcont/shell").selectItem "0002", "Column2"
    session.findById("wnd[0]/shellcont/shell").ensureVisibleHorizontalItem "0002", "Column2"
    session.findById("wnd[0]/shellcont/shell").pressButton "0002", "Column2"
    session.findById("wnd[1]/usr/cntlGUI_CONTAINER/shellcont/shell/shellcont[0]/shell")
    .selectedNode   = " 16"
    session.findById("wnd[1]/usr/cntlGUI_CONTAINER/shellcont/shell/shellcont[1]/shell")
    .modifyCell 0, "UNAME", Excel.application.Range("B3").Value
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press
'start the forwarding sequence
    session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").pressButton "EXPAND"
'hide details
    session.findById("wnd[0]/tbar[1]/btn[8]").press
'expand
     session.findById("wnd[0]/shellcont/shell/shellcont[0]/shell/shellcont[0]/shell").
    pressContextButton "XXXX"

    session.findById("wnd[0]/shellcont/shell/shellcont[0]/shell/shellcont[0]/shell").
    selectContextMenuItem " 2"
'find awaiting receipt row
    session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").pressButton "FIND"
    session.findById("wnd[1]/usr/txtG_FIND_STRING").Text = "689"
    session.findById("wnd[1]/tbar[0]/btn[71]").press


' need a way to tab or select the right workitem as this is the step that I cannot figure out
'    THIS IS WHERE I NEED HELP TO CLICK ON THE NUMBER LEFT OF 689
 
' Everything below here works

   session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").selectItem " 13", "Column2"
   session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").ensureVisibleHorizontalItem " 13",
     "Column2"
    session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").pressKey "Enter"
    session.findById("wnd[0]/mbar/menu[2]/menu[9]").Select
    session.findById("wnd[0]/shellcont/shell").selectItem "0016", "Column2"
    session.findById("wnd[0]/shellcont/shell").ensureVisibleHorizontalItem "0016", "Column2"
    session.findById("wnd[0]/shellcont/shell").pressButton "0016", "Column2"
    session.findById("wnd[1]/usr/ctxtPCHDY-SEARK").Text = ThisWorkbook.Sheets("AROtoARO").Cells(i,
    2).Value 'New ARO

    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[0]/shellcont/shell").selectItem "0020", "Column2"
    session.findById("wnd[0]/shellcont/shell").ensureVisibleHorizontalItem "0020", "Column2"
    session.findById("wnd[0]/shellcont/shell").pressButton "0020", "Column2"
    'reset
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/n"
    session.findById("wnd[0]").sendVKey 0

    Next i
     MsgBox "Script for 'AROtoARO'completed, brother!"
End Sub

Can anyone assist or have an idea ?

I tried several combos of sendvkeys and sendkeys {TAB} but the cursor remains in the left hand corner "find" box. Also a few posting on the subject I tried.

Upvotes: 0

Views: 293

Answers (0)

Related Questions