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.
can anybody help how to close the all new opened excel file. What code should i add in it
Dim ws As Worksheet
Dim lrow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim lastrow As Long
lastrow = ws.UsedRange.Rows.Count
If Not IsObject(SAPApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
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, "on"
wscript.ConnectObject Application, "on"
End If
lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lrow
If ws.Cells(i, "G") = True Then
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nfbl3n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radX_AISEL").Select
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").Text = ws.Range("A" & i)
session.findById("wnd[0]/usr/ctxtSD_SAKNR-HIGH").Text = ws.Range("B" & i)
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").Text = ws.Range("C" & i)
session.findById("wnd[0]/usr/ctxtSD_BUKRS-HIGH").Text = ws.Range("D" & i)
session.findById("wnd[0]/usr/ctxtSO_BUDAT-LOW").Text = ws.Range("E" & i)
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").Text = ws.Range("F" & i)
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").SetFocus
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = Cells(1, "K").Value
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i, "H").Value
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[11]").press
End If
Next i
End Sub'''
Everything is evolving. Also the possibilities that were known up to now. Recently you could also try the following. for example:
...
session.findById("wnd[1]/tbar[0]/btn[0]").press
'-------------new-------------------------------------------------------
'Is it really a number = 1 or a parameter = i?
myPath = Cells(1, "K").Value
myFileName = Cells(i, "H").Value
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = myPath
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = myFileName
'not necessary and with a file name shorter than 10 it would even be wrong
'session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[11]").press
myCount = Workbooks.Count
Do
DoEvents
Application.Wait Now + TimeSerial(0, 0, 5)
DoEvents
If Workbooks.Count > myCount Then Exit Do
Loop
Dim xlApp As Object
Set xlApp = GetObject(myPath & "\" & myFile).Application
For j = 1 To Workbooks.Count
If LCase(Workbooks(j).Name) = LCase(myFile) Then Exit For
Next j
xlApp.Workbooks(j).Close SaveChanges:=False
Set xlApp = Nothing
'--------------new-------------------------------------------------------
Next i
...
Regards, ScriptMan
Any body know how to handle this -How to Stop the Pop-up; SAP GUI Security using VBA script while export the excel
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.