-2

Below is the details that are required to input

Selection Criteria "Company Code"- Variable to pick from Cell A2 Selection Criteria" Document Number"- Click Multiple Selection by Field then Copy Data from Cell C2 till last cell with data in Column C and paste using Upload from Clipboard Filed Name "Layout"- Use /1130020N Field Name "Maximum no. of hits"- Leave this field blank Now execute and export the file to "C:\Users\nitemit\Desktop\SDM Folder\1130020\ZN_BSEG.xlsx"

I am getting error code "619 " for Company Code " session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").Text = companyCode"

Below is the complete code

Option Explicit
Public SapGuiAuto, WScript
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession

Sub RunSAPTransaction()
    Dim companyCode As String
    Dim documentNumbers As String
    
    ' Get the company code from Sheet1, Cell A2 in the same workbook
    companyCode = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
        
    ' Get the document numbers from Sheet1, starting from Cell C2 to the last cell with data in Column C
    documentNumbers = Join(Application.Transpose(ThisWorkbook.Sheets("Sheet1").Range("C2:C" & ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row).Value), ",")
    
    ' Connect to SAP
    Set SapGuiAuto = GetObject("SAPGUI")
    Set objGui = SapGuiAuto.GetScriptingEngine
    Set objConn = objGui.Children(0)
    Set session = objConn.Children(0)
    
    ' Maximize the SAP window
    session.findById("wnd[0]").Maximize
    
    ' Run transaction ZN_BSEG
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/nZN_BSEG"
    session.findById("wnd[0]").sendVKey 0
    
    ' Wait for the SAP session to be ready
    Do While session.ActiveWindow.Name = ""
        Application.Wait Now + TimeValue("0:00:01")
    Loop
    
    ' Enter the company code
    session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").Text = companyCode
    
    ' Click on "Multiple Selection by Field" for document numbers
    session.findById("wnd[0]/usr/txtS_BELNR-LOW").SetFocus
    session.findById("wnd[0]/usr/txtS_BELNR-LOW").caretPosition = 10
    session.findById("wnd[0]").sendVKey 2
    
    ' Paste document numbers from clipboard
    session.findById("wnd[1]/tbar[0]/btn[24]").press
    session.findById("wnd[1]/tbar[0]/btn[30]").press
    
    ' Wait for the SAP session to be ready
    Do While session.ActiveWindow.Name = ""
        Application.Wait Now + TimeValue("0:00:01")
    Loop
    
    ' Choose layout /1130020N
    session.findById("wnd[0]/usr/ctxtS_LAYOUT").Text = "/1130020N"
    
    ' Leave Maximum no. of hits blank
    
    ' Execute the transaction
    session.findById("wnd[0]").sendVKey 8
    
    ' Wait for the SAP session to be ready
    Do While session.ActiveWindow.Name = ""
        Application.Wait Now + TimeValue("0:00:01")
    Loop
    
    ' Export the file to the specified location
    session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").PressToolbarButton "&MB_VARIANT"
    session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectContextMenuItem "&LOAD"
    session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").CurrentCellColumn = "TEXT"
    session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = "0"
    session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").ClickCurrentCell
    session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").setCurrentCell 1, "BSCHL"
    session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").contextMenu
    session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectContextMenuItem "&XXL"
    
    ' Save the file
    session.findById("wnd[1]/tbar[0]/btn[11]").press
    session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\nitemit\Desktop\SDM Folder\1130020"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "ZN_BSEG.xlsx"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    
    ' Close SAP
    session.findById("wnd[0]/tbar[0]/btn[15]").press
End Sub
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Nitz
  • 1
  • 1
    First of all, nobody knows `ZN_BSEG` but you. Second, the error 619 means that the screen field doesn't exist in the screen behind `ZN_BSEG`. Stack Overflow is full of same questions, all going to the same direction that only you can fix it (i.e. the field name doesn't represent a field in the screen). – Sandra Rossi May 18 '23 at 06:15

1 Answers1

0

First, I would test whether you can omit the dynamic loop.

e.g.

 . . .
 ' Wait for the SAP session to be ready
   'Do While session.ActiveWindow.Name = ""
    Application.Wait Now + TimeValue("0:00:04")
  'Loop

' Enter the company code
session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").Text = companyCode
. . .

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9