title: SAP GUI Macro doesnt work when I had more than one session open at the same time
body: <p>I had develop a macro which search some information in SAP(SAP GUI)and copy it in a excel worksheet, but sadly it only works if I had open ONLY ONE session.
  Even if I try to create a new session by code("session.createSession"), the next steps(the important ones) are done in the first session that had been open before, for instance, it enters a code in a text box that doesn't exist in that screen and as a consecuence it appears an error.
  I know I can solve this by closing all sessions before run this macro, but I do believe that there is a better option for this. Hope you guys can help me with this.</p>
  
  <p>This is the macro:</p>
  
  <pre><code>&gt; Sub SAP()
  
  'Macro desarrollada por Marketing
  Dim application
  Dim connection
  
  If Not IsObject(application) Then
     Set SapGuiAuto = GetObject("SAPGUI")
     Set application = SapGuiAuto.GetScriptingEngine
  End If
  If Not IsObject(connection) Then
     Set connection = application.Children(0)
  End If
  If Not IsObject(session) Then
     Set session = connection.Children(0)
  End If
  If IsObject(WScript) Then
     WScript.ConnectObject session, "on"
     WScript.ConnectObject application, "on"
  
  
  End If
  Sheets("Carga").Select
  Range("A2", Range("P" &amp; Rows.Count).End(xlUp).Address).Sort Key1:=[K2], Order1:=xlAscending, Header:=xlYes
  
  Sheets("Carga").Select
  Range("a2").Select
  Selection.Copy
  Sheets("Consulta").Select
  Range("c5").Select
  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  Sheets("Carga").Select
  Range("m2").Select
  Selection.Copy
  Sheets("Consulta").Select
  Range("c6").Select
  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  'Código y producto
      Sheets("Consulta").Select
      Range("B9").Select
      Sheets("Carga").Select
      Range("b2:C2").Select
      Range(Selection, Selection.End(xlDown)).Select
      Selection.Copy
      Sheets("Consulta").Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  'lote y paquete
      Sheets("Carga").Select
      Range("e2:F2").Select
      Range(Selection, Selection.End(xlDown)).Select
      Selection.Copy
      Sheets("Consulta").Select
      Range("g9").Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  'Peso y Unidad
  Sheets("Carga").Select
      Range("G2:H2").Select
      Range(Selection, Selection.End(xlDown)).Select
      Selection.Copy
      Sheets("Consulta").Select
      Range("i9").Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  'Entrega
  Sheets("Carga").Select
      Range("k2").Select
      Range(Selection, Selection.End(xlDown)).Select
      Selection.Copy
      Sheets("Consulta").Select
      Range("D9").Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  
  'Sheets("Carga").Select
   '   Range("M2").Select
    '  Range(Selection, Selection.End(xlDown)).Select
     ' Selection.Copy
      'Sheets("Consulta").Select
      'Range("I9").Select
      'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  
  
  'Copiar todos los valores de las remesas y eliminar los que se repiten
  Sheets("Consulta").Select
  Range("d9").Select
      Range(Selection, Selection.End(xlDown)).Select
      Selection.Copy
      Sheets("BusquedaSAP").Select
      Range("A2").Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  'Eliminar las remesas duplicadas
  
  ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
  
  
  'Comenzar la búsqueda de Factura SAP a partir de las remesas
  
  Sheets("BusquedaSAP").Select
  For K = 2 To Cells(Rows.Count, 1).End(xlUp).Row
  
  'Si no hay datos, parar la macro
  
  If IsEmpty(Range("A" &amp; K)) Then
  
  'Volver a la página de inicio de SAP
  
  session.findById("wnd[0]/tbar[0]/btn[12]").press
  
  Exit Sub
  
  Sheets("Consulta").Select
  
  End If
  
  session.findById("wnd[0]").maximize
  session.findById("wnd[0]/tbar[0]/okcd").Text = "VL03N"
  session.findById("wnd[0]").sendVKey 0
  session.findById("wnd[0]/usr/ctxtLIKP-VBELN").Text = Sheets("BusquedaSAP").Range("A" &amp; K).Text
  session.findById("wnd[0]").sendVKey 0
  'Click en Flujo de Documentos(F7)
  session.findById("wnd[0]/tbar[1]/btn[7]").press
  'la segunda siempre es la remesa
  session.findById("wnd[0]/usr/shell/shellcont[1]/shell[1]").selectItem "          2", "&amp;Hierarchy"
  session.findById("wnd[0]/tbar[1]/btn[8]").press
  session.findById("wnd[0]/tbar[1]/btn[8]").press
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07").Select
  guiasider = session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV50A:2110/txtLIKP-XBLNR").Text
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV50A:2110/txtLIKP-XBLNR").SetFocus
  Worksheets("BusquedaSAP").Range("C" &amp; K) = guiasider
  session.findById("wnd[0]/tbar[0]/btn[3]").press
  session.findById("wnd[0]/tbar[0]/btn[3]").press
  
  'Click en Botón buscar
  session.findById("wnd[0]/usr/shell/shellcont[1]/shell[0]").pressButton "&amp;FIND"
  'Ingresar "Documento Contable en el campo de búsqueda
  session.findById("wnd[1]/usr/txtLVC_S_SEA-STRING").Text = "GT - Factura"
  'Dar click en aceptar varias veces
  session.findById("wnd[1]/tbar[0]/btn[0]").press
  session.findById("wnd[1]/tbar[0]/btn[0]").press
  session.findById("wnd[1]").Close
  session.findById("wnd[0]/tbar[1]/btn[8]").press
  
  'Buscar numero de Factura SAP
  facturaSAP = session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text
  session.findById("wnd[0]/usr/ctxtVBRK-VBELN").SetFocus
  Worksheets("BusquedaSAP").Range("B" &amp; K) = facturaSAP
  session.findById("wnd[0]/tbar[0]/btn[3]").press
  'Si es la primera remesa que busque todos los datos
  If K = 2 Then
  'Click en la lupa
  
  session.findById("wnd[0]/usr/shell/shellcont[1]/shell[1]").selectItem "          1", "&amp;Hierarchy"
  session.findById("wnd[0]/tbar[1]/btn[8]").press
  session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD").press
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07").Select
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/cmbGV_FILTER").SetFocus
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/cmbGV_FILTER").Key = "PAREH"
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/tblSAPLV09CGV_TC_PARTNER_OVERVIEW/txtGVS_TC_DATA-REC-NAME1[2,0]").SetFocus
  session.findById("wnd[0]").sendVKey 2
  'Tomar valor de cliente
  cliente = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").Text
  session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").SetFocus
  cliente1 = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").Text
  session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").SetFocus
  session.findById("wnd[1]/tbar[0]/btn[0]").press
  session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/tblSAPLV09CGV_TC_PARTNER_OVERVIEW/txtGVS_TC_DATA-REC-NAME1[2,3]").SetFocus
  session.findById("wnd[0]").sendVKey 2
  destinatario = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").Text
  session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").SetFocus
  destinatario1 = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").Text
  session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").SetFocus
  session.findById("wnd[0]/tbar[0]/btn[12]").press
  session.findById("wnd[0]/tbar[0]/btn[12]").press
  'tomar valor de empresa de transportes
  session.findById("wnd[0]/usr/shell/shellcont[1]/shell[1]").selectItem "          3", "&amp;Hierarchy"
  session.findById("wnd[0]/tbar[1]/btn[8]").press
  'En esta parte se selecciona la empresa de transporte, pero no todos lo tienen y arroja un error, por eso no se considera
  'session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_PR/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1021/btnB_PR_ADR").press
  'empresatransporte = session.findById("wnd[1]/usr/txtADDR1_DATA-NAME1").Text
  'session.findById("wnd[1]/usr/txtADDR1_DATA-NAME1").SetFocus
  'empresatransporte1 = session.findById("wnd[1]/usr/txtADDR1_DATA-NAME2").Text
  'session.findById("wnd[1]/usr/txtADDR1_DATA-NAME2").SetFocus
  'session.findById("wnd[0]/tbar[0]/btn[12]").press
  'Selecciona el nombre del transportista
  transportista = session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_PR/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1021/ctxtVTTK-EXTI1").Text
  session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_PR/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1021/ctxtVTTK-EXTI1").SetFocus
  
  
  Worksheets("BusquedaSAP").Range("d2") = cliente &amp; " " &amp; cliente1
  Worksheets("BusquedaSAP").Range("e2") = destinatario &amp; " " &amp; destinatario1
  Worksheets("BusquedaSAP").Range("f2") = empresatransporte &amp; " " &amp; empresatransporte1
  Worksheets("BusquedaSAP").Range("g2") = transportista
  session.findById("wnd[0]/tbar[0]/btn[15]").press
  session.findById("wnd[0]/tbar[0]/btn[15]").press
  session.findById("wnd[0]/tbar[0]/btn[15]").press
  End If
  
  
  session.findById("wnd[0]/tbar[0]/btn[3]").press
  session.findById("wnd[0]/tbar[0]/btn[12]").press
  session.findById("wnd[0]/tbar[0]/btn[12]").press
  
  
  Next K
  
  
  Sheets("Consulta").Activate
  
  Range("e9").Select
  Selection.Copy
  
  For i = 9 To Cells(Rows.Count, 4).End(xlUp).Row
  
  
  If Range("B" &amp; i) &lt;&gt; Empty Then
  
  If i = 9 Then
  
  Range("A" &amp; i) = 1
  
  
  Else
  
  
  Range("A" &amp; i) = Range("A" &amp; i - 1) + 1
  Range("e9").Select
  Selection.Copy
  Range("E" &amp; i).Select
      Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
          SkipBlanks:=False, Transpose:=False
  Range("f9").Select
  Selection.Copy
  Range("f" &amp; i).Select
      Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
          SkipBlanks:=False, Transpose:=False
  
  End If
  End If
  
  Next i
  
  Sheets("Consulta").Activate
  Range("O16").Select
  
   Selection.Copy
  Range(Cells(Rows.Count, 5).End(xlUp).Offset(1, -4), Cells(39, 13)).Select
  Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  
   Range("a9:m9").Select
  Selection.Copy
  Range(Cells(10, 1), Cells(Rows.Count, 5).End(xlUp).Offset(0, 8)).Select
  Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  
  Range("a10").Select
  
  'application.CutCopyMode = False
  
  End Sub
  </code></pre>
  
  <p>Cheers </p>