private void btn_run_Click(object sender, EventArgs e) { if (tb_id.Text == "" || tb_plant.Text == "") { return; } if (tb_id.TextLength > 4) { MessageBox.Show("ID item not valid", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (tb_plant.TextLength != 4) { MessageBox.Show("Centro inválido", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } //INICIAR ALTERAÇÃO #region if (dataGridView1.RowCount == 1) { return; } try { //Get the Windows Running Object Table SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper(); //Get the ROT Entry for the SAP Gui to connect to the COM object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); //Get the reference to the Scripting Engine object engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null); //Get the reference to the running SAP Application Window GuiApplication GuiApp = (GuiApplication)engine; //Get the reference to the first open connection GuiConnection connection = (GuiConnection)GuiApp.Connections.ElementAt(0); //get the first available session GuiSession session = (GuiSession)connection.Children.ElementAt(0); //Get the reference to the main "Frame" in which to send virtual key commandss //GuiFrameWindow frame = session.ActiveWindow; GuiFrameWindow frame = (GuiFrameWindow)session.FindById("wnd[0]"); //((GuiFrameWindow)frame.FindById("wnd[0]")); ((GuiOkCodeField)frame.FindById("wnd[0]/tbar[0]/okcd")).Text = "/nztvc025"; frame.SendVKey(0); int linhas1 = dataGridView1.RowCount; int i; ((GuiTextField)frame.FindById("wnd[0]/usr/ctxtP_PRFID")).Text = "0001"; ((GuiTextField)frame.FindById("wnd[0]/usr/ctxtP_WERKS")).Text = tb_plant.Text; for (i = 0; i < linhas1 - 1; i++) { try { ((GuiButton)frame.FindByName("btn[8]", "GuiButton")).Press(); string material = dataGridView1.Rows[i].Cells[0].Value.ToString(); int tam = material.Length; string component1 = dataGridView1.Rows[i].Cells[1].Value.ToString(); if (tam == 11) { ((GuiTextField)session.ActiveWindow.FindById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[1,21]")).Text = material; } else { ((GuiTextField)session.ActiveWindow.FindById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]")).Text = material; } ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press(); ((GuiToolbarControl)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[0]")).PressButton("&FIND"); ((GuiTextField)session.ActiveWindow.FindById("wnd[1]/usr/txtLVC_S_SEA-STRING")).Text = tb_id.Text.ToString(); ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press(); ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press(); if (session.Children.Count != 2) { dataGridView1.Rows[i].Cells[2].Value = "N/A"; goto acabou; } ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press(); for (int j = 1; j < 100; j++) { string esp; if (j < 10) { esp = " "; } else { esp = " "; } ((GuiTree)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[1]")).SelectItem(esp + j, "&Hierarchy"); ((GuiTree)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[1]")).EnsureVisibleHorizontalItem(esp + j, "&Hierarchy"); ((GuiTree)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[1]")).DoubleClickItem(esp + j, "&Hierarchy"); if (((GuiCTextField)session.ActiveWindow.FindByName("ZTBVC_657-ID_ITEM", "GuiCTextField")).Text == tb_id.Text.ToString()) { ((GuiToolbarControl)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[0]")).PressButton("EDIT_ESP"); ((GuiTextField)frame.FindById("wnd[0]/usr/subSUBSCR_ESPEC:SAPLZRVC_ZBOM_POS_ESP:0300/subSUBSCR_ESP:SAPLZRVC_ZBOM_POS_ESP:0340/ctxtZTBVC_658-IDNRK")).Text = component1; ((GuiButton)frame.FindByName("btn[8]", "GuiButton")).Press(); dataGridView1.Rows[i].Cells[2].Value = "OK"; break; } } acabou : ((GuiButton)frame.FindByName("btn[2]", "GuiButton")).Press(); } catch { ((GuiButton)frame.FindByName("btn[2]", "GuiButton")).Press(); } #endregion } MessageBox.Show("Alteração Concluída", "Alteração Concluída", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } catch { MessageBox.Show("Erro ao conectar com SAP", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
public TSapControl FindByName <TSapControl>(string name) { return(window.FindByName <TSapControl>(name)); }