在Excel VBA中禁用SAP登录弹窗

3

我有一个VBA代码,可以帮助登录到SAP GUI。代码工作正常,但在建立连接时出现警告弹窗。

脚本正在打开与系统的连接:*******

enter image description here

我需要绕过或禁用此警告弹窗。我已经编写了代码,但它不起作用。请帮忙。

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

请注意:我知道在SAP GUI中可以禁用此弹出窗口,但出于安全考虑,不建议这样做,因为这可能会导致未来的安全威胁。 请提供以下代码的帮助和建议:
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

1
该警告是由SAP客户端弹出的,以警示计算机用户某些潜在恶意脚本正在代表他们执行操作。我不认为有一种方法可以禁用它,因为这将规避SAP客户端实施的安全措施来警示用户。 - JNevill
3个回答

6

这些是注册表中可以按照您的需求关闭和打开的设置。 我有一个名为clsSapgui的类用于此操作。

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

然后您可以完全关闭警告。
Sub Silence()

    Dim mySapGui As New clsSapGui

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

End Sub

并使用以下方法重新启动它们:

    Sub Show_Warnings()

        Dim mySapGui As New clsSapGui

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

End Sub

如果您愿意,当然可以向类中添加新的方法,这样会更简洁。

类clsRegistry看起来是这样的

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

2

这很方便。也许OP可以编写脚本来切换,这样用户只需要按一次按钮。不过,我会警告他们不要这样做,因为这会为用户关闭安全特性,可能会在将来引起恶意脚本无需提示而执行的情况。 - JNevill
我同意,这很麻烦,但我明白为什么要有这个功能。是的,请谨慎使用。 - JCalvano
问题与“安全”设置无关,而是与同一SAP GUI选项的“可访问性和脚本 > 脚本”设置有关。 - undefined

1
让我来补充一下Storax关于Windows注册表中SAP GUI条目的答案,用文字而不是仅仅是代码来解释。
Storax的答案是通过脚本将下面两个SAP GUI选项设置为关闭/取消勾选,并恢复它们之前的值。

SAP GUI Options > Accessibility & Scripting > Scripting

来自指南“SAP GUI管理 - 文档版本:7.70 PL03 – 2021-06-16”的官方描述(https://help.sap.com/doc/6ceeb0cbf06540d18c116f060f0669aa/770.03/en-US/sap_gui_administration.pdf):
“当脚本连接到SAP GUI时通知”
[HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP Frontend Server\Security] WarnOnAttach` (REG_DWORD) [默认值:1] {0 = 不活动; 1 = 活动}
“当脚本打开连接时通知”
[HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP Frontend Server\Security] WarnOnConnection (REG_DWORD) [默认值:1] {0 = 不活动; 1 = 活动}
Windows注册表(regedit.exe):

SAP GUI Options Windows Registry


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接