Verbindung mit SAP
Fettertiger
das untenstehende Makro treibt mich noch zur Verzweiflung. Mit dem Makro starte ich über einen SAP shortcut eine SAP session, mache ein paar Transaktionen und schliesse per SAP Befehle auch SAP wieder.
Das Makro funzt soweit auch einwandfrei, ausser wenn ich es zum zweiten Mal aufrufe. Dann bekomme ich in dieser Zeile einen Runtimeerror 91: wscript.ConnectObject Session, "on" .
Hat jemand eine Idee wie man das lösen kann?
Dim SapApplication, SapGuiAuto, Connection, Session, wscript As Object
Dim lastrow As Double
Public System As String
Sub SAP_prepare_sapscript()
Application.EnableCancelKey = xlDisabled
If Left(tbL_Set.Range("c3").Value, 1) = "%" Then
PathStrg = Environ(Mid(tbL_Set.Range("c3").Value, 2, InStr(2, tbL_Set.Range("c3").Value, _
"%", 0) - 2))
Else:
PathStrg = tbL_Set.Range("c3").Value
End If
If Right(PathStrg, 1) "\" Then PathStrg = PathStrg & "\"
On Error Resume Next
Shell (tbL_Set.Range("c4").Value & "sapshcut.exe " & PathStrg & System & ".sap")
If Wait_for_Window("SAP Easy Access") = False Then GoTo giveUp 'Separates Makro das wartet _
bis SAP geöffnet ist
On Error GoTo 0
If Not IsObject(SapApplication) Then
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
If Err.Number 0 Then
MsgBox ("Not able to log in")
NoSap = True
Err.Clear
GoTo giveUp
End If
Set SapApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SapApplication.Children(0)
End If
If Not IsObject(Session) Then
Set Session = Connection.Children(0)
End If
If IsObject(wscript) Then
wscript.ConnectObject Session, "on"
wscript.ConnectObject Application, "on"
End If
Err.Clear
'Set up connection to the Excel data to be keyed into SAP:
Session.findById("wnd[0]").maximize
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
run_Sap_Script
Application.ScreenUpdating = False
If Err.Number 0 Then
If Err.Number 91 Then
If Err.Number 9 Then
MsgBox ("There was an error running the Script")
Sapok = False
Err.Clear
GoTo giveUp
End If
End If
Err.Clear
End If
giveUp:
Application.WindowState = xlMaximized
End Sub
Sub run_Sap_Script()
'mach ganz viel tolle Sachen in SAP
'log back off out of SAP
Session.findById("wnd[0]/tbar[0]/btn[15]").press
Session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
Err.Clear
End Sub
Auf jeden Fall schon mal vielen Dank im Voraus
Grüße
Theo