Page 1 of 1

Sap Script VBA ขึ้น Run-time error 619 The control could not be found by id

Posted: Sat Jan 18, 2020 12:08 pm
by ch.jew10
เรียน อาจารย์ ครับ
ผมได้บันทึก script ที่ระบบ Sap และนำ code มาใส่ใน excel VBA ความต้องการคือ เรียก transection vl02n ของ Sap
จากนั้นให้นำค่าใน sheet ตั้งแต่ cell A2 ไปใส่ใน Sap แล้วกด post และกลับมานำค่า cell A3 มาทำเช่นเดิม วนไปจนครบทุกบรรทัด
ที่มีในคอลัมภ์ A ทดลองที่เครื่องผมเป็น Excel 2013 งสามารถใช้งานได้ตามที่ต้องการ แต่เครื่อง user อื่น เป็น excel 2010 พบปัญหาใช้งานไม่ได้
แสดง error 619 The control could not be found by id ไฮ้ไลท์สีเหลือง อยู่ที่บรรทัด session.findById("wnd[0]/usr/ctxtLIKP-VBELN").Text = Cells(i, 1)
ขอปรึกษาว่าจะต้องแก้ไข code อย่างไรให้สามารถใช้งานได้เช่นกันครับ ขอบคุณมากครับ

Code: Select all

Public Sub SimpleSAPVL02N()
Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
  Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
  Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
  Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
  
  Dim LR As Long
  Dim i As Integer
  
  LR = Range("A" & Rows.Count).End(xlUp).Row
  
  i = 2
  
  For i = 2 To LR
       
      'Move sloc
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvl02n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtLIKP-VBELN").Text = Cells(i, 1)
session.findById("wnd[0]/usr/ctxtLIKP-VBELN").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[20]").press

'for loop
      
   Next i
        
End Sub

Re: Sap Script VBA ขึ้น Run-time error 619 The control could not be found by id

Posted: Sat Jan 18, 2020 4:13 pm
by snasui
ch.jew10 wrote: Sat Jan 18, 2020 12:08 pm แสดง error 619 The control could not be found by id ไฮ้ไลท์สีเหลือง อยู่ที่บรรทัด session.findById("wnd[0]/usr/ctxtLIKP-VBELN").Text = Cells(i, 1)
:D บรรทัดนี้ไม่มีอะไรผิดปกติครับ หากสร้าง Object SapGuiAuto ขึ้นมาได้ก็น่าจะรู้จักทุก Control

ลองตรวจสอบว่า User ใช้ SAP อยู่กี่หน้าจอ หากมากกว่า 1 ให้ปิดทิ้งไปเสียก่อนให้เหลือแต่ 1 จอแล้วทดสอบดูใหม่ครับ

Re: Sap Script VBA ขึ้น Run-time error 619 The control could not be found by id

Posted: Sat Jan 18, 2020 4:40 pm
by ch.jew10
ขอบคุณมากครับ ผมจะลองดูอีกทีครับ

Re: Sap Script VBA ขึ้น Run-time error 619 The control could not be found by id

Posted: Thu Jan 23, 2020 12:04 am
by ch.jew10
เรียน อาจารย์
ผมลองบันทึก script ที่เครื่อง user ด้วยการกระทำเดียวกัน แต่ดูเหมือนว่า code ที่ระบุตำแหน่ง แตกต่างกันครับ
จึงเข้าใจว่าน่าจะเป็นสาเหตุที่ทำให้ไม่สามารถใช้งาน script แบบเดียวกับที่เครื่องผมใช้ได้
รบกวนสอบถามว่าพอจะแนะนำได้ไหมครับว่าต้องไปตั้งค่าตรงไหนให้ใช้ได้เหมือนกันครับ
ตัวอย่างครับ

script เครื่อง User บันทึกได้

Code: Select all

session.findById("wnd[0]/usr/txt[2]").text = "6401"
session.findById("wnd[0]/usr/txt[3]").text = "8000"
Script ที่เครื่องผมบันทึกได้

Code: Select all

session.findById("wnd[0]/usr/txtGS_DATA_100-WERKS").Text = "6401"
session.findById("wnd[0]/usr/txtGS_DATA_100-LGORT_FRM").Text = Cells(i, 4)

Re: Sap Script VBA ขึ้น Run-time error 619 The control could not be found by id

Posted: Thu Jan 23, 2020 7:07 pm
by snasui
:D ผมเคยเจอกรณีของ QA กับ Production แสดง Object ไม่เหมือนกันอยู่บ้างเหมือนกันแต่เนื่องจากทำงานกับ Production จึงให้ Record Script จากตัว Production เลยจึงไม่ต้องเขียน Code ดักจับ Object ที่หลากหลาย

สำหรับกรณีนี้คงต้องลองใช้การดักจับ Object เข้ามาช่วยแทน ยกตัวอย่างเช่นด้านล่างครับ

Code: Select all

if session.findById("wnd[0]/usr/txtGS_DATA_100-WERKS") is nothing then
    session.findById("wnd[0]/usr/txt[2]").text = "6401"
    session.findById("wnd[0]/usr/txt[3]").text = "8000"
else
    session.findById("wnd[0]/usr/txtGS_DATA_100-WERKS").Text = "6401"
    session.findById("wnd[0]/usr/txtGS_DATA_100-LGORT_FRM").Text = Cells(i, 4)
end if