on 07-29-2011 2:11 PM
Hi everyone,
until now I usually asked users to log - in into SAP first and then use some Excel macros to download /modify data in SAP.
I have now found a neat way of opening SAP by opening a SAP shortcut - which works like a charm. Unfortunately that Charm has a very limited life span - because you are only able to run the macro once. If I want to rerun the macro I receive a Runtimeerror 91: wscript.ConnectObject Session, "on" .
This is the code I am using:
Dim SapApplication, SapGuiAuto, Connection, Session, wscript As Object
Public System As String
Sub SAP_prepare_sapscript()
System = "PL1"
Application.EnableCancelKey = xlDisabled
PathStrg = "C:\Temp\"
On Error Resume Next
Shell ("C:\Program Files\SAP\FrontEnd\SAPgui\"sapshcut.exe " & PathStrg & System & ".sap")
If Wait_for_Window("SAP Easy Access") = False Then GoTo giveUp 'Separate Macro which waits until SAP is open
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
Session.findById("wnd[0]").maximize
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()
'here I am running my normal SAP commands like opening transactions....
'log out of SAP
Session.findById("wnd[0]/tbar[0]/btn[15]").press
Session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
Err.Clear
End Sub
Any Idea what needs to be changed?
Thanks for your help
Theo
Edited by: Fettertiger on Jul 29, 2011 3:11 PM
Hi Theo,
I know the Problem ...
Usually it gets me everytime I want to restart a Macro when i've changed som lines for the first time...
Found out that it is better to Logon with a new Window / Logon
This can be done by using this code:;
If Not IsObject(SAPguiApp) Then
Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If Not IsObject(Connection) Then
Set Connection = SAPguiApp.OpenConnection("SYSTEM", True)
End If
If Not IsObject(Session) Then
Set Session = Connection.Children(0)
End If
Session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "PL1"
Session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = inputbox("Benutzer")
Session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = inputBox("Passwort")
Session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "DE"
Session.findById("wnd[0]/usr/txtRSYST-LANGU").SetFocus
Session.findById("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2
Session.findById("wnd[0]").sendVKey 0
This always works ...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thomas,
while the log in really seems to work this way, I struggle with some other issue using your script:
The SAP window that will pop-up will look slightly diffferent from the "normal" SAP window, as there is no Command field, nor the little arrow to "unhide" the command field. This is really annoying, as this prevents me from running my transactions.
Regards
Theo
Hi Theo
thats the problem with the new Designs
It seems not to be there, but it is..
You can call your transactions by using this little snippet
sapsession.findbyid("wnd[0]/tbar[0]/okcd").text ="md04" 'for example
sapsession.findbyid("wnd[0]").sendvkey 0
Edited by: Thomas Br on Aug 2, 2011 10:25 AM
This might help someone like me.
wscript.disConnectObject Session
wscript.disConnectObject Application
https://www.vbsedit.com/html/5c3b18d5-f137-4a93-b9d2-3aa005fa3b2f.asp
At the end of the loop add these two lines. So you wont get the runtime error 91.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Theo,
There is no right answer, but to make it run the code, it would also help.
. . .
on error resume next
If IsObject(wscript) Then
wscript.ConnectObject Session, "on"
wscript.ConnectObject Application, "on"
End If
on error goto 0
. . .
Regards,
ScriptMan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.