kämpfe jetzt seit zwei Tagen gegen ein Problem und komme allein nicht weiter. Brauche Hilfe.
Im Folgenden wird eine Datei " "bvtaeglichms.xlsx" erzeugt, diese soll kurz geöffnet werden um Daten rauszukopieren.
Anschließend soll die Datei geschlossen und gelöscht werden. Wobei nicht mal das schließen klappt.
Die Datei geht am Ende immer nochmal auf... Ich dreh noch durch, weiß nicht warum.
Wenn ich erst lösche meckert er das die Datei nicht mehr existieren würde und öffnet mir einfach eine neue Blanko Excel.
Das möchte ich alles nicht...
Einfach zu machen und löschen.
Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
Public objSheet As Worksheet
Sub DatenHolen()
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Dim selectedBetrieb As String
Dim selectedDatum As String
Dim selectedPfad As String
selectedBetrieb = ThisWorkbook.Worksheets("BV").Range("N23").Value
selectedDatum = ThisWorkbook.Worksheets("BV").Range("N24").Value
selectedPfad = ThisWorkbook.Path
session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nmb51"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/tbar[1]/btn[17]").Press
session.FindById("wnd[1]/usr/txtV-LOW").Text = "BVTAEGLICHMS"
session.FindById("wnd[1]/usr/txtENAME-LOW").Text = ""
session.FindById("wnd[1]/usr/txtV-LOW").CaretPosition = 12
session.FindById("wnd[1]/tbar[0]/btn[8]").Press
session.FindById("wnd[0]/usr/ctxtWERKS-LOW").Text = selectedBetrieb
session.FindById("wnd[0]/usr/ctxtBUDAT-LOW").Text = selectedDatum
session.FindById("wnd[0]/usr/ctxtBUDAT-LOW").SetFocus
session.FindById("wnd[0]/usr/ctxtBUDAT-LOW").CaretPosition = 6
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/tbar[1]/btn[8]").Press
session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").SetCurrentCell 9, "MENGE"
session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectedRows = "9"
session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").ContextMenu
session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectContextMenuItem "&XXL"
session.FindById("wnd[1]/tbar[0]/btn[0]").Press
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = ThisWorkbook.Path
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "bvtaeglichms.xlsx"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").CaretPosition = 17
session.FindById("wnd[1]/tbar[0]/btn[11]").Press
ThisWorkbook.Worksheets("BV").Range("A6:I9999").ClearContents
Dim ext_wb As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ext_wb = Workbooks.Open(ThisWorkbook.Path & "\bvtaeglichms.xlsx")
ext_wb.Sheets("Sheet1").Range("A1:I9999").Copy
ThisWorkbook.Sheets("BV").Range("A5").PasteSpecial
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ext_wb.Close SaveChanges:=True
'Kill ThisWorkbook.Path & "\bvtaeglichms.xlsx"
End Sub