3

I have a VBA code which helps to login into SAP GUI. The code works fine but I am getting a warning popup when it establishes the connection.

A script is opening a connection to system: *******

enter image description here

I need to either bypass or disable this warning popup. I have written the code but it is not working. Please help.

Sub code1()
If Not IsObject(SAPguiApp) Then
    Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If

If Not IsObject(Connection) Then
    Set Connection = SAPguiApp.OpenConnection("********", True)
End If

If Not IsObject(Session) Then
    Set Session = Connection.Children(0)
End If
If Session.ActiveWindow.Name = "wnd[1]" Then
    If Session.findbyid("wnd[1]").Text Like "A script*" Then Session.findbyid("wnd[0]/usr/btnSPOP-OPTION1").press
End If

Session.findbyid("wnd[0]/usr/txtRSYST-MANDT").Text = "103"
Session.findbyid("wnd[0]/usr/txtRSYST-BNAME").Text = "*****"
Session.findbyid("wnd[0]/usr/txtRSYST-LANGU").SetFocus
Session.findbyid("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2
Session.findbyid("wnd[0]").sendVKey 0

Session.findbyid("wnd[0]/tbar[0]/okcd").Text = "/nsu01"
Session.findbyid("wnd[0]").sendVKey 0
Session.findbyid("wnd[0]").maximize

End Sub

Please note: I know that this popup can be disabled in SAP GUI but not in a favour to do the same because it can lead to a security threat in future. Please advise something to do with the help of a code like below :

If Session.ActiveWindow.Name = "wnd[1]" Then
    If Session.findbyid("wnd[1]").Text Like "A script*" Then
        Session.findbyid("wnd[0]/usr/btnSPOP-OPTION1").press
    End If
End If
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Anirudh Chauhan
  • 111
  • 2
  • 9
  • That warning is popped by SAP client to alert the computer user that some potentially bad script is acting on their behalf. I don't believe there is a way to disable it as that would be circumventing the security put in place by the SAP client to alert the user. – JNevill Dec 13 '18 at 14:48

2 Answers2

5

These are settings in the registry which you can turn off and turn on like you want I have a class clsSapgui for that

Option Explicit
Const mRegNameBase = "HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP Frontend Server\Security\"
Const mUserScripting = "UserScripting"
Const mWarnOnAttach = "WarnOnAttach"
Const mWarnOnConnection = "WarnOnConnection"
Const mSecurityLevel = "SecurityLevel"
Dim mRegKey As New clsRegistry

Property Get UserScripting() As Boolean
    UserScripting = ReadRegKey(mUserScripting)
End Property

Property Let UserScripting(newVal As Boolean)
    WriteRegKey mUserScripting, CBoolToVal(newVal)
End Property

Property Get WarnOnAttach() As Boolean
    WarnOnAttach = ReadRegKey(mWarnOnAttach)
End Property

Property Let WarnOnAttach(newVal As Boolean)
    WriteRegKey mWarnOnAttach, CBoolToVal(newVal)
End Property

Property Get WarnOnConnection() As Boolean
    WarnOnConnection = ReadRegKey(mWarnOnConnection)
End Property

Property Let WarnOnConnection(newVal As Boolean)
    WriteRegKey mWarnOnConnection, CBoolToVal(newVal)
End Property
Property Get SecurityLevel() As Boolean
    SecurityLevel = ReadRegKey(mSecurityLevel)
End Property

Property Let SecurityLevel(newVal As Boolean)
    WriteRegKey mSecurityLevel, CBoolToVal(newVal)
End Property
Private Function CBoolToVal(bVal As Boolean) As Byte
    If bVal Then
        CBoolToVal = 1
    Else
        CBoolToVal = 0
    End If
End Function

Private Function ReadRegKey(sRegValue As String) As String

    Dim sRegName As String

On Error GoTo NoRegkey

    sRegName = mRegNameBase & sRegValue
    ReadRegKey = mRegKey.ReadRegKey(sRegName)
    Exit Function

NoRegkey:
    ReadRegKey = 0

End Function

Private Function WriteRegKey(sRegKey As String, ByVal sRegValue As String) As Boolean

    Dim sRegName As String

On Error GoTo NoRegkey

    sRegName = mRegNameBase & sRegKey
    WriteRegKey = mRegKey.WriteRegKey(sRegName, sRegValue, "REG_DWORD")
    Exit Function

NoRegkey:
    WriteRegKey = False

End Function

And then you can turn off the warnings completely

Sub Silence()

    Dim mySapGui As New clsSapGui

    With mySapGui
        .UserScripting = True
        .SecurityLevel = False
        .WarnOnAttach = False
        .WarnOnConnection = False
    End With

End Sub

And you turn them on again with

    Sub Show_Warnings()

        Dim mySapGui As New clsSapGui

        With mySapGui
            .UserScripting = True
            .SecurityLevel = True
            .WarnOnAttach = True
            .WarnOnConnection = True
        End With

End Sub

If you want to you can of course add new methods to the class which would be neater

The class clsRegistry looks like that

Option Explicit

Function ReadRegKey(RegKey As String) As Variant

Dim wsh As Object

    Set wsh = CreateObject("WScript.Shell")

    On Error GoTo NoRegkey

    ReadRegKey = wsh.regread(RegKey)

    Set wsh = Nothing
    Exit Function

NoRegkey:
    ReadRegKey = ""

End Function

Function DeleteRegKey(RegKey As String) As Boolean
' http://msdn.microsoft.com/en-us/library/yfdfhz1b(v=vs.84).aspx
Dim wsh As Object

   Set wsh = CreateObject("WScript.Shell")

   On Error GoTo NoRegkey

   wsh.RegDelete RegKey
   DeleteRegKey = True
   Set wsh = Nothing
   Exit Function

NoRegkey:
    DeleteRegKey = False

End Function


Function WriteRegKey(RegName As String, RegValue As Variant, RegType As String) As Boolean
' http://msdn.microsoft.com/en-us/library/yfdfhz1b(v=vs.84).aspx
Dim wsh As Object

   Set wsh = CreateObject("WScript.Shell")

   On Error GoTo NoRegkey

   wsh.RegWrite RegName, RegValue, RegType
   WriteRegKey = True
   Set wsh = Nothing
   Exit Function

NoRegkey:
    WriteRegKey = False

End Function
Storax
  • 11,158
  • 3
  • 16
  • 33
3

the setting is in the SAP GUI. Under the security folder.

SAP Gui Screenshot

https://blogs.sap.com/2015/06/09/tips-stop-the-pop-up-sap-gui-security-remeber-my-decision/

JCalvano
  • 55
  • 1
  • 5
  • That's pretty handy. Perhaps OP can script to toggle that so the user only has to hit the button once. Although, I would caution them from doing so since it's programatically shutting off a security feature for the user which may invite nefarious scripts to act without prompt in the future. – JNevill Dec 13 '18 at 15:26
  • I agree, is a pain but I see why this feature is there. Yes please use with caution. – JCalvano Dec 13 '18 at 18:16