I'm trying to automate SAP GUI export to an Excel file, which in turn is then used by another Excel file with some VBA code to automate filtering and formatting the data.
I've got it all running, except for one (seemingly minor) problem: SAP GUI always opens the exported Excel file automatically and there seems to be no way of stopping this as it seems to happen AFTER the subroutines to import the Data in Excel VBA have finished (they contain the SAP GUI script).
If I only run the sap_export
subroutine, then the Excel file opens, which is simply annoying. But if I run refresh_sap()
, which calls sap_export()
, followed by refresh()
, which accesses the exported Excel file importing data, I get the prompt telling me that the file is already in use.
I have found no way to stop the file from being opened by the SAP GUI script as it does not seem to happen during run time. I suspect that this is why I couldn't find any way to use Application.Wait
or DoEvents
to solve this. However long I wait, it will not work, as the file is simply never open until after runtime.
Sub refresh_sap()
Call sap_export
Call refresh
End Sub
Sub refresh()
'refreshes the connection to the SAP-exported Excel-file
ActiveWorkbook.Connections("export").refresh
'deleting unwanted data
ActiveWorkbook.Sheets("PC-Liste komplett").Select
Selection.AutoFilter
ActiveSheet.ListObjects("Tabelle_export").Range.AutoFilter Field:=4, Criteria1:="Löschen"
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.EntireRow.Delete
ActiveSheet.ListObjects("Tabelle_export").Range.AutoFilter Field:=4
Range("A1").Select
End Sub
Sub sap_export()
Dim set0 As Integer
Dim set1 As String
Dim set2 As Boolean
'vbs-script recorded with the SAP-GUI
If Not IsObject(sapp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Scon) Then
Set Scon = sapp.Children(0)
End If
If Not IsObject(session) Then
Set session = Scon.Children(0)
End If
If IsObject(WScript) Then
WScript.connectobject session, "on"
WScript.connectobject sapp, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n KE5X"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtGT_PRCTR-LOW").Text = "*"
session.findById("wnd[0]").sendVKey 8
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
If session.findById("wnd[1]/usr/radRB_1").Selected = True Then
set0 = 0
ElseIf session.findById("wnd[1]/usr/radRB_2").Selected = True Then
set0 = 1
ElseIf session.findById("wnd[1]/usr/radRB_OTHERS").Selected = True Then
set0 = 2
End If
set1 = session.findById("wnd[1]/usr/cmbG_LISTBOX").Key
set2 = session.findById("wnd[1]/usr/chkCB_ALWAYS").Selected
session.findById("wnd[1]/usr/radRB_OTHERS").Select
session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "10"
session.findById("wnd[1]/usr/chkCB_ALWAYS").Selected = False
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "S:\FIN-Alle\Kostenstellen - Innenauftragsliste\SAP"
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
Select Case set0
Case 0
session.findById("wnd[1]/usr/radRB_1").Select
Case 1
session.findById("wnd[1]/usr/radRB_2").Select
Case 2
session.findById("wnd[1]/usr/radRB_OTHERS").Select
End Select
session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = set1
session.findById("wnd[1]/usr/chkCB_ALWAYS").Selected = set2
session.findById("wnd[1]").sendVKey 12
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n"
session.findById("wnd[0]").sendVKey 0
End Sub
As I have the impression that I can do nothing to close the file within the subroutines (as it only opens after the run time) I'm currently looking for a way to:
- either tell SAP software/SAP GUI not to open the file at all,
- or to prohibit it from being able to access Excel,
- or maybe just close SAP GUI altogether and see if that works - although I would prefer not to do that.