SAP Export - File wird nicht geschlossen
14.02.2025 21:43:48
xbelightx
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