SAP-Forum.ORG
   
This web site isn't affiliated with, sponsored by, or approved by SAP AG. Is only a SAP users community.

Go Back   SAP-Forum.ORG > SAP Forums > SAPScript - Smartforms

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tab Alignment in SAP SCRIPT Mirna SAPScript - Smartforms 2 08-05-2011 09:31 PM
Sap gui 7.20 macropus SAP Manuals 0 12-16-2010 03:42 AM
Java Script in BSP application koko SAP Manuals 0 12-03-2010 04:14 PM
Stop SAP script Pepa SAP BASIS 2 08-26-2010 01:30 PM
SAP Gui hangs Golfi SAP BASIS 4 06-06-2010 03:59 PM

Aprenda SAP!
Reply
 
Thread Tools Display Modes
  #1  
Old 08-15-2011, 09:32 PM
ERP ERP is offline
Senior Member
 
Join Date: May 2010
Location: Rhode Island
Posts: 231
Default Run GUI script more than once

Hi all,


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:

Code:
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
ERP
Reply With Quote
Sponsors
  #2  
Old 08-15-2011, 09:33 PM
Portal Portal is offline
Senior Member
 
Join Date: May 2010
Location: Miami
Posts: 791
Default Re: Run GUI script more than once

Hi ERP,

There is no right answer, but to make it run the code, it would also help.


Code:
on error resume next
If IsObject(wscript) Then
        wscript.ConnectObject Session, "on"
        wscript.ConnectObject Application, "on"
End If
on error goto 0
.


Regards,
Portal
Reply With Quote
  #3  
Old 08-15-2011, 09:35 PM
Mirna Mirna is offline
Senior Member
 
Join Date: May 2010
Location: Alabama
Posts: 267
Default Re: Run GUI script more than once

Hi ERP,

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:


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.

Regards,
Mirna
Reply With Quote
Entre a los Links relacionados
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 10:19 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Forum Design By inferno