title: Close sap export multiple excel file with vba
body: <p>This is my code for SAP GUI script in which on click of button i am exporting a data to file . i have mutiple rows there would be 100's of file are generated. i have to manually close all file.</p>
  <p>can anybody help how to close the all new opened excel file.
  What code should i add in it</p>
  <pre><code>Dim ws As Worksheet
  Dim lrow As Long
  Set ws = ThisWorkbook.Worksheets(&quot;Sheet1&quot;)
  Dim lastrow As Long
  lastrow = ws.UsedRange.Rows.Count
  
  If Not IsObject(SAPApp) Then
     Set SapGuiAuto = GetObject(&quot;SAPGUI&quot;)
     Set SAPApp = SapGuiAuto.GetScriptingEngine
  End If
  If Not IsObject(Connection) Then
     Set Connection = SAPApp.Children(0)
  End If
  If Not IsObject(session) Then
     Set session = Connection.Children(0)
  End If
  If IsObject(wscript) Then
     wscript.ConnectObject session, &quot;on&quot;
     wscript.ConnectObject Application, &quot;on&quot;
  End If
  
  lrow = ws.Cells(Rows.Count, &quot;A&quot;).End(xlUp).Row
  
  For i = 2 To lrow
  If ws.Cells(i, &quot;G&quot;) = True Then
  
  session.findById(&quot;wnd[0]&quot;).maximize
  session.findById(&quot;wnd[0]/tbar[0]/okcd&quot;).Text = &quot;/nfbl3n&quot;
  session.findById(&quot;wnd[0]&quot;).sendVKey 0
  session.findById(&quot;wnd[0]/usr/radX_AISEL&quot;).Select
  session.findById(&quot;wnd[0]/usr/ctxtSD_SAKNR-LOW&quot;).Text = ws.Range(&quot;A&quot; &amp; i)
  session.findById(&quot;wnd[0]/usr/ctxtSD_SAKNR-HIGH&quot;).Text = ws.Range(&quot;B&quot; &amp; i)
  session.findById(&quot;wnd[0]/usr/ctxtSD_BUKRS-LOW&quot;).Text = ws.Range(&quot;C&quot; &amp; i)
  session.findById(&quot;wnd[0]/usr/ctxtSD_BUKRS-HIGH&quot;).Text = ws.Range(&quot;D&quot; &amp; i)
  session.findById(&quot;wnd[0]/usr/ctxtSO_BUDAT-LOW&quot;).Text = ws.Range(&quot;E&quot; &amp; i)
  session.findById(&quot;wnd[0]/usr/ctxtSO_BUDAT-HIGH&quot;).Text = ws.Range(&quot;F&quot; &amp; i)
  session.findById(&quot;wnd[0]/usr/ctxtSO_BUDAT-HIGH&quot;).SetFocus
  session.findById(&quot;wnd[0]/usr/ctxtSO_BUDAT-HIGH&quot;).caretPosition = 10
  session.findById(&quot;wnd[0]/tbar[1]/btn[8]&quot;).press
  session.findById(&quot;wnd[0]/mbar/menu[0]/menu[3]/menu[1]&quot;).Select
  session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
  session.findById(&quot;wnd[1]/usr/ctxtDY_PATH&quot;).Text = Cells(1, &quot;K&quot;).Value
  session.findById(&quot;wnd[1]/usr/ctxtDY_FILENAME&quot;).Text = Cells(i, &quot;H&quot;).Value
  session.findById(&quot;wnd[1]/usr/ctxtDY_FILENAME&quot;).caretPosition = 10
  session.findById(&quot;wnd[1]/tbar[0]/btn[11]&quot;).press
  End If
  Next i
  End Sub'''
  </code></pre>