cancel
Showing results for 
Search instead for 
Did you mean: 

Run VBS script when logged through SAP shortcut

Former Member
0 Kudos

Hi Experts,

I have created a desktop shortcut to access the SAP ECC environment, similar as described on this wiki: https://wiki.scn.sap.com/wiki/display/ABAP/SAPGUI+shortcuts

Then, when I do the login through this shortcut, the script below simple does not work.


If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.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

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/nMM03"

session.findById("wnd[0]").sendVKey 0

The error is:


Windows Script Host

Script: C:\temp\Script5.vbs

Line: 2

Charact.: 4

Error: Invalid Syntax

Code: 800401E4

Source: (null)

It seems that the GetObject("SAPGUI") function returns Nothing, then the Set statement does not work.

When I do the login through saplogon, it works just fine.

Has anyone experienced this before? Any ideas?

Tks

Darley

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Darley,

welcome in the Scripting Language forum.

I wouldn't use a SAP GUI Shortcut, I would use a normal Windows Desktop Right Click Menu New > Shortcut.

There is probably one reason:

You are using a 64-bit Windows (x64) and with the shortcut you start also the x64 Windows Scripting Host, but the 64-bit WSH can't use 32-bit (x86) objects. With the standard Windows menu you can use this:


Choose in your shortcut C:\Windows\SysWOW64\wscript.exe and the name of your script.

If you don't want to start the SAP Logon explicite, you can also use another way:

'-Begin-----------------------------------------------------------------


  '-Sub Main------------------------------------------------------------

    Sub Main()
      sapServer = "/H/10.100.200.150/S/3200" 'Insert here your IP and Port
      Set sap = CreateObject("Sapgui.ScriptingCtrl.1")

      Set sapCon = sap.OpenConnectionByConnectionString(sapServer, True)

      If IsObject(sapCon) Then
        MsgBox "Connection successful"
      Else
        MsgBox "Connection failed"
      End If
      sapCon.Close
    End Sub

  '-Main----------------------------------------------------------------
    Main()

'-End-------------------------------------------------------------------


Try it and let us know your results.


Cheers

Stefan

Former Member
0 Kudos

Hello Stefan,

Thank you for the tips.

I did create the shortcut with right click >> new >> Sap GUI Shortcut.

Yes, I would like to avoid opening SAP Logon to have the benefits of the shortcut

Regarding the first option, to use 64-bit WSH, did not work. I got exactly the same error message.

          c:\Windows\SysWOW64\wscript.exe c:\temp\Script5.vbs

Then I tried also the 32-bit WSH - same error.

          c:\Windows\System32\wscript.exe c:\temp\Script5.vbs

The other option, to use the method OpenConnectionByConnectionString worked, it got a new instance of SAP Gui and start to interact with the script.

However, it did not use the current open connection/session, I was prompted again with username and password to a new login on the same environment.

Is there a way of accessing current connection instead of opening a new one? I had a look on the GuiApplication object and did not find it.

Regards

Darley

stefan_schnell
Active Contributor
0 Kudos

Hello Darley,

try this script:

OpenNewConnFlag = False

If Not IsObject(application) Then

  On Error Resume Next

  'If the script can't get the object, it starts saplogon

  'and try to get the object again.

  Set SapGuiAuto  = GetObject("SAPGUI")

  If Err.Number <> 0 Then

    On Error Goto 0

    Set WshShell = CreateObject("WScript.Shell")

    Set oExec = WshShell.Exec(_

      "c:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")

    Do While Not wshShell.AppActivate("SAP Logon 740") 

      WScript.Sleep 500 

    Loop

    Set SapGuiAuto  = GetObject("SAPGUI")

  Else

    On Error Goto 0

  End If

  Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

  On Error Resume Next

  'If the script can't open connection 0, it opens a

  'new connection

  Set connection = application.Children(0)

  If Err.Number <> 0 Then

    On Error Goto 0

    application.OpenConnectionByConnectionString _

      "/H/10.100.200.100/S/3200", True

    OpenNewConnFlag = True

  Else

    On Error Goto 0

  End If

  Set connection = application.Children(0)

End If

If Not IsObject(session) Then

  Set session = connection.Children(0)

  'If the script opens a new connection, it logon to the system

  If OpenNewConnFlag = True Then

    session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "001"

    session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "DARLEY"

    session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "minisap"

    session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "EN"

    session.findById("wnd[0]").sendVKey 0   

  End If

End If

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/nMM03"

session.findById("wnd[0]").sendVKey 0

You can start this script from the command line via

c:\Windows\SysWOW64\wscript.exe Darley.vbs

Let us know your results.

Cheers

Stefan

Former Member
0 Kudos

Hello Stefan

Well, it's one way of doing. After adjusted the connection string, username and pwd worked.

But still is not my first intention.

I already have an opened connection (through shortcut) and would like to avoid opening a new one. Some users even cannot open two connection on the same environment.

Case this is the only option, I will open connections only by saplogon, avoid use shortcuts, to share same connection.

In my opinion, this is a design mistake: the COM object is registered at ROT only when saplogon is running. As we are able to open SAP connections without saplogon, the object should be there anytime we have SAPgui (or SAPFRONT) running.

Thank you anyway.

Darley

stefan_schnell
Active Contributor
0 Kudos

Hello Darley,

it is possible to disable the start of the SAP Logon - read note 844095 - and to use sapgui.exe.

Try

reg query HKLM\SOFTWARE\SAP\SAPGUI

at your command line. If you see

HKEY_LOCAL_MACHINE\SOFTWARE\SAP\SAPGUI

    StartSapLogon    REG_DWORD    0x0

it is disabled.

I think it is not a design mistake, it is a question of the administration settings.

Let us know your results.

Cheers

Stefan

stefan_schnell
Active Contributor
0 Kudos

Hello Darley,

you can find the solution of your problem in note 552646.

Symptom: If you start SAP GUI for Windows via Sapshortcut (without changing the registry key described), it's not possible to use SAP GUI Scripting.

Solution: Set the value of the key StartSapLogon to 1.

Let us know your results.

Cheers

Stefan

Former Member
0 Kudos

Great!

Solved by setting the key below to 1

     HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\SAP\SAPGUI\

     StartSaplogon = 1

Tks,

Darley

Answers (0)