SAP Script über VBA beenden
16.08.2023 06:36:47
Nikki
Ich habe mir mit Hilfe des Internets ein SAP Sript zusammengebastelt welches über Excel gestartet wird. Das Marko speichert Daten aus SAP in Excel ab. Jetzt habe ich das Problem, dass wenn unten stehendes Marko durchgelaufen ist, kein anderes mehr funktioniert. Entweder bekomme ich eine Fehlermeldung oder die Buttons mit den Markos reagieren überhaupt nicht mehr. Ich muss erst die Datei schließen und wieder neu öffnen. Danach funktionieren wieder alle Makros. Ich vermute, dass es irgendwie an der Verbindung zu SAP liegt. Wie bekomme ich das hin, ohne jedesmal die Datei neu zu öffnen? Kann mir jemand helfen? Das wäre super. Vielen dank für eure Hilfe.
Sub Vorgänge()
Dim SAPGuiAuto, Application, connection, session As Object
Dim Stückliste As String
Dim Speichername As String
Dim Speicherort As String
Dim i As Integer
Dim lastrow As Integer
Set SAPGuiAuto = GetObject("SAPGUI")
Set Application = SAPGuiAuto.getscriptingEngine
Set connection = Application.Children(0)
Set session = connection.Children(0)
lastrow = ThisWorkbook.Sheets("PPS").Cells(Rows.Count, 1).End(xlUp).Row
session.findById("wnd[0]").resizeWorkingPane 342, 42, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "/ncoois"
session.findById("wnd[0]").sendVKey 0
For i = 2 To lastrow
Stückliste = ThisWorkbook.Sheets("PPS").Range("A" & i).Value
Speichername = ThisWorkbook.Sheets("PPS").Range("b" & i).Value
Speicherort = ThisWorkbook.Sheets("PPS").Range("c" & i).Value
session.findById("wnd[0]").resizeWorkingPane 272, 40, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "/ncoois"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ssub%_SUBSCREEN_TOPBLOCK:PPIO_ENTRY:1100/chkPPIO_ENTRY_SC1100-SELECT_PLANNEDORDS").SetFocus
session.findById("wnd[0]/usr/ssub%_SUBSCREEN_TOPBLOCK:PPIO_ENTRY:1100/chkPPIO_ENTRY_SC1100-SELECT_PLANNEDORDS").Selected = True
session.findById("wnd[0]/usr/ssub%_SUBSCREEN_TOPBLOCK:PPIO_ENTRY:1100/cmbPPIO_ENTRY_SC1100-PPIO_LISTTYP").Key = "PPIOA000"
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_MATNR-LOW").Text = Stückliste
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_MATNR-LOW").SetFocus
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_MATNR-LOW").caretPosition = 9
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont[1]/shell").currentCellRow = -1
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont[1]/shell").selectColumn "AUFNR"
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont[1]/shell").contextMenu
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/usr/chkCB_ALWAYS").SetFocus
session.findById("wnd[1]/usr/chkCB_ALWAYS").Selected = True
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").SetFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 0
session.findById("wnd[1]").sendVKey 4
session.findById("wnd[2]/usr/ctxtDY_PATH").Text = Speicherort
session.findById("wnd[2]/usr/ctxtDY_FILENAME").Text = Speichername
session.findById("wnd[2]/usr/ctxtDY_FILENAME").caretPosition = 9
session.findById("wnd[2]/tbar[0]/btn[11]").press
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]").resizeWorkingPane 179, 40, False
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_MATNR-LOW").SetFocus
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_MATNR-LOW").caretPosition = 9
Next i
Myend:
Set session = Nothing
Set SAPGuiAuto = Nothing
Set Application = Nothing
Set connection = Nothing
End Sub