Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Anzeige
Inhaltsverzeichnis

SAP Export - File wird nicht geschlossen

Forumthread: SAP Export - File wird nicht geschlossen

SAP Export - File wird nicht geschlossen
14.02.2025 21:43:48
xbelightx
Guten Abend zusammen,

seit Stunden versuche ich folgendes Problem zu lösen und, wenn ich mir die Google Beiträge durchlese, scheint es als ob es vielen so geht...
Ich habe einen Vorgang im SAP gescriptet und in meinem VBA Code integriert. Folgendes soll ausgeführt werden:
1. Macro: Exportiere aus SAP eine Datei im XLSX Format und speichere die Datei in meinem C:\Temp Ordner.
2. Macro: Importiere die Daten aus dem SAP Export in eine XLSM Datei, wenn bestimmte Kriterien erfüllt sind und schließe die XLSX Datei am Ende.

Der SAP Export ( XLSX Datei schließt sich aber nicht, ganz im Gegenteil: sie wird erst nach dem Ausführen der beiden Makros wirklich sichtbar. So wie ich es bis jetzt verstanden habe, exportiert SAP die Datei in einer anderen Instanz und wird erst sichtbar, wenn die Makros durchlaufen wurden... ( falls ich da etwas falsch verstanden habe, könnt ihr mich gerne aufschlauen. ) Also kurz gesagt: Workbooks("name.xlsx").close funktioniert hier nicht, auch eine Zeitverzögerung funktioniert nicht.

Der Code läuft auch nicht auf Fehler. Er führt alles so aus wie Eingegeben, nur er schließt die XLSX Datei am Ende nicht.

Gibt es hier im Forum jemand, der damit Erfahrung hat?

Hier die 2 Codes:

Sub testdownload()

Dim sapGuiAuto, Application, connection, session As Object

Set sapGuiAuto = GetObject("SAPGUI")
Set Application = sapGuiAuto.GetScriptingEngine
Set connection = Application.Children(0)
Set session = connection.Children(0)

session.findById("wnd[0]").resizeWorkingPane 131, 44, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvl06f"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtV-LOW").Text = "XNG DOT"
session.findById("wnd[1]/usr/txtENAME-LOW").Text = ""
session.findById("wnd[1]/usr/txtENAME-LOW").SetFocus
session.findById("wnd[1]/usr/txtENAME-LOW").caretPosition = 0
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[18]").press
session.findById("wnd[0]/mbar/menu[3]/menu[3]/menu[0]").Select
session.findById("wnd[0]/mbar/menu[3]/menu[3]/menu[1]").Select
session.findById("wnd[0]/tbar[1]/btn[33]").press
session.findById("wnd[1]/tbar[0]/btn[71]").press
session.findById("wnd[2]/usr/txtRSYSF-STRING").Text = "/XNG_DOT"
session.findById("wnd[2]/usr/txtRSYSF-STRING").caretPosition = 7
session.findById("wnd[2]/tbar[0]/btn[0]").press
session.findById("wnd[3]/usr/lbl[14,2]").SetFocus
session.findById("wnd[3]/usr/lbl[14,2]").caretPosition = 2
session.findById("wnd[3]").sendVKey 2
session.findById("wnd[1]/usr/lbl[1,3]").SetFocus
session.findById("wnd[1]/usr/lbl[1,3]").caretPosition = 4
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/usr/lbl[14,1]").SetFocus
session.findById("wnd[0]/usr/lbl[14,1]").caretPosition = 5
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/mbar/menu[0]/menu[5]/menu[1]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "c:\temp"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "XNG DOT TRACKING SAP.XLSX"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 20
session.findById("wnd[1]/tbar[0]/btn[11]").press

Call CompareAndCopyData

End Sub

Sub CompareAndCopyData()

Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceLastRow As Long
Dim targetLastRow As Long
Dim i As Long
Dim isUnique As Boolean
Dim cell As Range

' Set reference to the source workbook and sheet
Set sourceWorkbook = Workbooks.Open("C:\temp\XNG DOT TRACKING SAP.xlsx")
Set sourceSheet = sourceWorkbook.Sheets("Sheet1") ' Name des Quellblatts

' Set reference to the target workbook and sheet
Set targetWorkbook = ThisWorkbook
Set targetSheet = targetWorkbook.Sheets("DOT Reporting")

' Find the last row with data in the source sheet
sourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row

' Find the last row in the target sheet to start appending data
targetLastRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row + 1

' Loop through the source rows starting from row 2
For i = 2 To sourceLastRow
isUnique = True

' Check if the combination of values from columns A and B already exists in the target sheet
For Each cell In targetSheet.Range("A2:A" & targetLastRow - 1)
If sourceSheet.Cells(i, 1).Value = cell.Value And _
sourceSheet.Cells(i, 2).Value = cell.Offset(0, 1).Value Then
isUnique = False

' Update the values in columns L and O if a duplicate is found
cell.Offset(0, 2).Value = sourceSheet.Cells(i, 3).Value ' Update column C
cell.Offset(0, 11).Value = sourceSheet.Cells(i, 12).Value ' Update column L
cell.Offset(0, 14).Value = sourceSheet.Cells(i, 15).Value ' Update column O
Exit For
End If
Next cell

' If the row is unique, copy it to the target sheet
If isUnique Then
sourceSheet.Cells(i, 1).Resize(, 19).Copy targetSheet.Cells(targetLastRow, 1)
targetLastRow = targetLastRow + 1
End If
Next i

' Die Datei, die angezeigt werden soll, öffnen
Dim wbToDisplay As Workbook
Set wbToDisplay = Workbooks.Open("C:\temp\XNG DOT TRACKING SAP.xlsx")

' Die Datei aktivieren
wbToDisplay.Activate

' Aktuelle Datei ohne Speichern schließen
Application.DisplayAlerts = False
Workbooks("XNG DOT TRACKING SAP.xlsx").Close SaveChanges:=False
Application.DisplayAlerts = True

' Meldung, dass der Vorgang abgeschlossen ist
MsgBox "Aktualisierungsvorgang abgeschlossen!"


End Sub

Viele Grüße
Anne
Anzeige

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: SAP Export - File wird nicht geschlossen
15.02.2025 14:14:16
ralf_b
gibt es einen besonderen Grund warum du die selbe Datei zweimal öffnest?
Set sourceWorkbook = Workbooks.Open("C:\temp\XNG DOT TRACKING SAP.xlsx")
Set wbToDisplay = Workbooks.Open("C:\temp\XNG DOT TRACKING SAP.xlsx")

und warum verwendest du nicht die Objektvariablen wenn du die Datei schließen willst, sondern holst dir die Dateireferenz aus der Workbooksauflistung. ?
Workbooks("XNG DOT TRACKING SAP.xlsx").Close SaveChanges:=False

Im Grunde kannst du dir das zweite Öffnen sparen. Es dürft reichen die Datei zu schließen.
sourceWorkbook.Close SaveChanges:=False

Und nur mal so nebenbei erwähnt. Die Targetdatei nach den Änderungen speichern wäre vielleicht ne gute Idee.
Anzeige
AW: SAP Export - File wird nicht geschlossen
17.02.2025 22:01:12
Ulf
Hi,
die SAP-Frage gab es schon mal in ähnlicher Form. Schliessen der Verbindungen und Objekte sollte helfen.
Den Vorschlag von Ralf eingebaut eribt sich


Sub lesen()
Call testdownload
Call CompareAndCopyData
End Sub

Sub testdownload()
Dim sapGuiAuto As Object
Dim sapApplication As Object
Dim sapConnection As Object
Dim sapSession As Object
Set sapGuiAuto = GetObject("SAPGUI")
Set sapApplication = sapGuiAuto.GetScriptingEngine
Set sapConnection = sapApplication.Children(0)
Set sapSession = sapConnection.Children(0)
With sapSession
.findById("wnd[0]").resizeWorkingPane 131, 44, False
.findById("wnd[0]/tbar[0]/okcd").Text = "/nvl06f"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/tbar[1]/btn[17]").press
.findById("wnd[1]/usr/txtV-LOW").Text = "XNG DOT"
.findById("wnd[1]/usr/txtENAME-LOW").Text = ""
.findById("wnd[1]/usr/txtENAME-LOW").SetFocus
.findById("wnd[1]/usr/txtENAME-LOW").caretPosition = 0
.findById("wnd[1]/tbar[0]/btn[8]").press
.findById("wnd[0]/tbar[1]/btn[8]").press
.findById("wnd[0]/tbar[1]/btn[18]").press
.findById("wnd[0]/mbar/menu[3]/menu[3]/menu[0]").Select
.findById("wnd[0]/mbar/menu[3]/menu[3]/menu[1]").Select
.findById("wnd[0]/tbar[1]/btn[33]").press
.findById("wnd[1]/tbar[0]/btn[71]").press
.findById("wnd[2]/usr/txtRSYSF-STRING").Text = "/XNG_DOT"
.findById("wnd[2]/usr/txtRSYSF-STRING").caretPosition = 7
.findById("wnd[2]/tbar[0]/btn[0]").press
.findById("wnd[3]/usr/lbl[14,2]").SetFocus
.findById("wnd[3]/usr/lbl[14,2]").caretPosition = 2
.findById("wnd[3]").sendVKey 2
.findById("wnd[1]/usr/lbl[1,3]").SetFocus
.findById("wnd[1]/usr/lbl[1,3]").caretPosition = 4
.findById("wnd[1]").sendVKey 2
.findById("wnd[0]/usr/lbl[14,1]").SetFocus
.findById("wnd[0]/usr/lbl[14,1]").caretPosition = 5
.findById("wnd[0]").sendVKey 2
.findById("wnd[0]/mbar/menu[0]/menu[5]/menu[1]").Select
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/usr/ctxtDY_PATH").Text = "c:\temp"
.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "XNG DOT TRACKING SAP.XLSX"
.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 20
.findById("wnd[1]/tbar[0]/btn[11]").press
End With
Set sapSession = Nothing
Set sapConnection = Nothing
Set sapApplication = Nothing
Set sapGuiAuto = Nothing
End Sub

Sub CompareAndCopyData()
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceLastRow As Long
Dim targetLastRow As Long
Dim i As Long
Dim isUnique As Boolean
Dim cell As Range
' Set reference to the source workbook and sheet
Set sourceWorkbook = Workbooks.Open("C:\temp\XNG DOT TRACKING SAP.xlsx")
Set sourceSheet = sourceWorkbook.Sheets("Sheet1") ' Name des Quellblatts
' Set reference to the target workbook and sheet
Set targetWorkbook = ThisWorkbook
Set targetSheet = targetWorkbook.Sheets("DOT Reporting")
' Find the last row with data in the source sheet
sourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
' Find the last row in the target sheet to start appending data
targetLastRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row + 1
' Loop through the source rows starting from row 2
For i = 2 To sourceLastRow
isUnique = True
' Check if the combination of values from columns A and B already exists in the target sheet
For Each cell In targetSheet.Range("A2:A" & targetLastRow - 1)
If sourceSheet.Cells(i, 1).Value = cell.Value And _
sourceSheet.Cells(i, 2).Value = cell.Offset(0, 1).Value Then
isUnique = False
' Update the values in columns L and O if a duplicate is found
cell.Offset(0, 2).Value = sourceSheet.Cells(i, 3).Value ' Update column C
cell.Offset(0, 11).Value = sourceSheet.Cells(i, 12).Value ' Update column L
cell.Offset(0, 14).Value = sourceSheet.Cells(i, 15).Value ' Update column O
Exit For
End If
Next cell
' If the row is unique, copy it to the target sheet
If isUnique Then
sourceSheet.Cells(i, 1).Resize(, 19).Copy targetSheet.Cells(targetLastRow, 1)
targetLastRow = targetLastRow + 1
End If
Next i
' Die Datei, die angezeigt werden soll, öffnen
''Dim wbToDisplay As Workbook
''Set wbToDisplay = Workbooks.Open("C:\temp\XNG DOT TRACKING SAP.xlsx")
' Die Datei aktivieren
'sourceWorkbook.Activate
sourceWorkbook.Close SaveChanges:=False
' Aktuelle Datei ohne Speichern schließen
Application.DisplayAlerts = False
'Workbooks("XNG DOT TRACKING SAP.xlsx").Close SaveChanges:=False
Application.DisplayAlerts = True
' Meldung, dass der Vorgang abgeschlossen ist
MsgBox "Aktualisierungsvorgang abgeschlossen!"
End Sub

hth
Ulf
Anzeige
AW: SAP Export - File wird nicht geschlossen
18.02.2025 15:02:49
xbelightx
Hallo Ulf,

vielen Dank für die Anpassungen.
Ich habe es gerade ausprobiert, aber leider hat es nicht gewünschten Effekt. Die Excel Export aus SAP schließt sich immer noch nicht automatisch.
Kannst du mir den Beitrag verlinken, in dem die Problematik schonmal thematisiert wurde?

VG Anne
Anzeige
AW: SAP Export - File wird nicht geschlossen
19.02.2025 03:14:58
Ulf
Hi Anne,
wenn du nach SAP im Archiv hier suchst, liefert das div. Ergebnisse.
Wahrscheinlich müssen Connection und / oder Session geschlossen werden, bevor die Objekte entladen werden. Denkbar ist ebenso, dass die Rechtevergabe seitens der Admins hier greift oder dem Zielordner nicht vertraut wird / den heruntergeladenen Dateien vertraut werden muss.
hth
Ulf
Anzeige

Forumthreads zu verwandten Themen

Anzeige