Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
0 Kudos
Often is here seen in the posts that the instantiation of the Sapgui.ScriptingCtrl.1 with the method OpenConnection is the reason of problems. Here is another way which bypass this problems by launching the SAP Logon. Also is it possible to terminate the SAP Logon.
'-Begin-----------------------------------------------------------------

'-Directives----------------------------------------------------------
Option Explicit

'-Variables-----------------------------------------------------------
Dim SAPLogon, SAPLogonTitle
SAPLogon = "saplogon.exe" : SAPLogonTitle = "SAP Logon 740"
'SAPLogon = "saplgpad.exe" : SAPLogonTitle = "SAP Logon Pad 740"
Dim SysDescr, SysIP
SysDescr = "NSP" : SysIP = "192.168.203.134"

'-Function FindProcess------------------------------------------------
Function FindProcess(ProcessName)

'-Variables-------------------------------------------------------
Dim WMIServ, Processes, Process

FindProcess = False

Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _
"impersonate}!\\.\root\cimv2")

Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _
"Where Name = '" & ProcessName & "'")

For Each Process In Processes
FindProcess = True
Exit Function
Next

End Function

'-Sub TerminateProcess------------------------------------------------
Sub TerminateProcess(ProcessName)

'-Variables-------------------------------------------------------
Dim WMIServ, Processes, Process

Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _
"impersonate}!\\.\root\cimv2")

Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _
"Where Name = '" & ProcessName & "'")

For Each Process In Processes
Process.Terminate()
Exit Sub
Next

End Sub

'-Function GetSAPGUIObject--------------------------------------------
Function GetSAPGUIObject()

'-Variables-------------------------------------------------------
Dim WshShell, Exec

If FindProcess(SAPLogon) Then
Set GetSAPGUIObject = GetObject("SAPGUI")
Else
Set WshShell = CreateObject("WScript.Shell")
Set Exec = WshShell.Exec(_
"c:\Program Files (x86)\SAP\FrontEnd\SAPgui\" & SAPLogon)
Do While Not WshShell.AppActivate(SAPLogonTitle)
WScript.Sleep 500
Loop
Set GetSAPGUIObject = GetObject("SAPGUI")
End If

End Function

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

'-Variables-------------------------------------------------------
Dim SapGuiAuto, Application, Connection, Session

Set SapGuiAuto = GetSAPGUIObject()
If Not IsObject(SapGuiAuto) Then
Exit Sub
End If

Set Application = SapGuiAuto.GetScriptingEngine
If Not IsObject(Application) Then
Set SapGuiAuto = Nothing
Exit Sub
End If

Set Connection = Application.OpenConnection(SysDescr, True)
'Set Connection = Application.OpenConnectionByConnectionString(_
' "/H/" & SysIP, True)
If Not IsObject(Connection) Then
Set Application = Nothing
Set SapGuiAuto = Nothing
Exit Sub
End If

Set Session = Connection.Children(0)
If Not IsObject(Session) Then
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing
Exit Sub
End If

MyScript(Session)

Set Session = Nothing
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing
TerminateProcess SAPLogon

End Sub

'-Sub MyScript--------------------------------------------------------
'-
'- Put your script into this sub routine
'-
'---------------------------------------------------------------------
Sub MyScript(Session)

'-Variables-------------------------------------------------------
Dim Info

Set Info = Session.Info

MsgBox "Transaction: " & Info.Transaction & vbCrLf & _
"Program: " & Info.Program

End Sub

'-Main----------------------------------------------------------------
Main

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

The function FindProcess is searching via the Windows Management Instrumentation (WMI) for the saplogon.exe process. If the process exists, we get the object SAPGUI from the Running Object Table (ROT). Otherwise we start a new instance of saplogon.exe and get the object SAPGUI. At the end we kill the process saplogon.exe via the sub procedure TerminateProcess.
Labels in this area