I am trying to get export from SAP. Once export done, i need to close the export file.I have written below code to execute the same thing, but my problem is that code is not waiting until file gets load and it is skipping the export file close. kindly help me to sort out this problem.
Sub FBL5N()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Dim sApplication
Dim session
Dim file As String
Dim objShell As Object
Dim strFolder As String
Dim wb As Workbook
Dim wbk As Workbook
Dim ws As Worksheet
Dim lrw As Integer
Dim Wshell As Object
Set objShell = CreateObject("Wscript.Shell")
strFolder = objShell.SpecialFolders("mydocuments") & "\Temp\"
file = strFolder & "\ARREPORT.xlsx"
If FileExists(file) Then 'See above
SetAttr file, vbNormal
On Error Resume Next
Kill file
On Error Resume Next
End If
If Not IsObject(sApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sApplication.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 sApplication, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nFBL5N"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtDD_KUNNR-LOW").Text = ThisWorkbook.Sheets("Formulated").Range("R9")
session.findById("wnd[0]/usr/ctxtDD_KUNNR-HIGH").Text = ThisWorkbook.Sheets("Formulated").Range("R10")
session.findById("wnd[0]/usr/ctxtDD_BUKRS-LOW").Text = ThisWorkbook.Sheets("Formulated").Range("R8")
session.findById("wnd[0]/usr/ctxtPA_STIDA").Text = ThisWorkbook.Sheets("Formulated").Range("R11")
session.findById("wnd[0]/usr/ctxtPA_VARI").Text = ThisWorkbook.Sheets("Formulated").Range("R12")
session.findById("wnd[0]/usr/ctxtPA_VARI").SetFocus
session.findById("wnd[0]/usr/ctxtPA_VARI").caretPosition = 12
session.findById("wnd[0]/mbar/menu[0]/menu[0]").Select
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = strFolder
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "export.xlsx"
session.findById("wnd[1]").sendVKey 0
Application.Wait Now() + TimeValue("00:00:01")
For Each wb In Application.Workbooks
If wb.Name = "export.xlsx" Then
wb.Close
End If
Next wb
End Sub