'SAP GUI script from Excel VBA Macro optimizing

I am trying to optimize my Excel VBA to SAP connection and don't want to click "OK" on two message boxes that appear when starting the following code:

 1 Sub SAP_1()
 2 
 3 Dim obj_Shell As Object
 4 Dim obj_SAPGUI As Object
 5 Dim obj_Application As Object
 6 Dim obj_Connection As Object
 7 Dim obj_session As Object
 8 
 9 Application.DisplayAlerts = False
10     Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
11     Set obj_Shell = CreateObject("WScript.Shell")
12     Do Until obj_Shell.AppActivate("SAP Logon")
13         application.Wait Now + TimeValue("0:00:01")                 
14     Loop
15     Set obj_Shell = Nothing
16     Set obj_SAPGUI = GetObject("SAPGUI")
17     Set obj_Application = obj_SAPGUI.GetScriptingEngine
18     Set obj_Connection = obj_Application.OpenConnection(str_ConName, True)
19     Set obj_session = obj_Connection.Children(0)
20 ' rest of the code
21 Application.DisplayAlerts = True
22 End Sub

How can I avoid the following SAP message boxes or click them via VBA:

Line 17: "A script tries to access SAP"

Line 18: "A script opens a connection to the following system: ..."

And what's the differents to the code below? Why is the SAP GUI Scripting asking not to define them as Objects? Is this a better alternative?

 1     If Not IsObject(obj_SAPGUI) Then
 2        Set obj_SAPGUI = GetObject("SAPGUI")
 3        Set obj_Application = obj_SAPGUI.GetScriptingEngine
 4     End If
 5     If Not IsObject(obj_Connection) Then
 6        Set obj_Connection = obj_Application.Children(0)
 7    End If
 8     If Not IsObject(obj_session) Then
 9        Set obj_session = obj_Connection.Children(0)
10     End If
11     If IsObject(obj_WScript) Then
12        obj_WScript.ConnectObject obj_session, "on"
13        obj_WScript.ConnectObject obj_Application, "on"
14     End If

Are there other things in the code that can be optimized?

Thank you for your help.



Solution 1:[1]

Thank you very much, that's my final code right now:

Function RegKeyExists(i_RegKey As String) As Boolean

Dim myWS As Object
Set myWS = CreateObject("WScript.Shell")

On Error GoTo ErrorHandler
myWS.RegRead i_RegKey
RegKeyExists = True
Exit Function
  
ErrorHandler:
RegKeyExists = False

End Function
'
'-----------------------------------------------------------------------

Sub RegKeyReset()

Dim obj_WS As Object
Dim RegKey1 As String
Dim RegKey2 As String
Dim RegKey3 As String

Set obj_WS = CreateObject("WScript.Shell")
    RegKey1 = "HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP Frontend Server\Security\UserScripting"
    RegKey2 = "HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP Frontend Server\Security\WarnOnAttach"
    RegKey3 = "HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP Frontend Server\Security\WarnOnConnection"

    ' RegKey1
    If RegKeyExists(RegKey1) = False Then
        Exit Sub
    Else
        obj_WS.RegWrite RegKey1, 1, "REG_DWORD"     ' Value = 1, Type = Boolean
    End If

    ' RegKey2
    If RegKeyExists(RegKey2) = False Then
        Exit Sub
    Else
        obj_WS.RegWrite RegKey2, 0, "REG_DWORD"     ' Value = 0, Type = Boolean
    End If

    ' RegKey3
    If RegKeyExists(RegKey3) = False Then
        Exit Sub
    Else
        obj_WS.RegWrite RegKey3, 0, "REG_DWORD"     ' Value = 0, Type = Boolean
    End If

End Sub
'
'-----------------------------------------------------------------------

Sub SAPTransaction()
Dim ...
Set ...

Call RegKeyReset    ' <--------------------------- Problem solved here...

' rest of the code

End Sub
'

I did it this way, because I wont be the only person/user to use the macro, so I don't have to tell everybody to change their settings in SAP.

Also thanks to: https://www.slipstick.com/developer/read-and-change-a-registry-key-using-vba/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1