来自 Excel VBA 宏优化的 SAP GUI 脚本
SAP GUI script from Excel VBA Macro optimizing
我正在尝试优化我的 Excel VBA 到 SAP 连接,不想在启动以下代码时出现的两个消息框中单击“确定”:
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
如何避免以下 SAP 消息框或通过 VBA 单击它们:
第 17 行:“脚本试图访问 SAP”
第 18 行:“脚本打开与以下系统的连接:...”
下面的代码有什么不同?为什么 SAP GUI 脚本要求不要将它们定义为对象?这是更好的选择吗?
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
代码中还有其他可以优化的地方吗?
感谢您的帮助。
为了避免脚本厌倦访问响应的消息。连接到 SAPGUI,您必须在注册表中或通过 SAPGUI 更改设置。
在 SAPGUI 中按 Alt-F12,然后按 select 选项,转到脚本,然后取消选中下面的所有复选框 启用脚本。
这些设置存储在注册表中,也可以使用 VBA 代码进行设置。关键是 HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP 前端 Server\Security\
非常感谢,这是我现在的最终代码:
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
'
我是这样做的,因为我不会是唯一一个person/user使用宏的人,所以我不必告诉每个人更改他们在 SAP 中的设置。
同时感谢:https://www.slipstick.com/developer/read-and-change-a-registry-key-using-vba/
我正在尝试优化我的 Excel VBA 到 SAP 连接,不想在启动以下代码时出现的两个消息框中单击“确定”:
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
如何避免以下 SAP 消息框或通过 VBA 单击它们:
第 17 行:“脚本试图访问 SAP”
第 18 行:“脚本打开与以下系统的连接:...”
下面的代码有什么不同?为什么 SAP GUI 脚本要求不要将它们定义为对象?这是更好的选择吗?
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
代码中还有其他可以优化的地方吗?
感谢您的帮助。
为了避免脚本厌倦访问响应的消息。连接到 SAPGUI,您必须在注册表中或通过 SAPGUI 更改设置。
在 SAPGUI 中按 Alt-F12,然后按 select 选项,转到脚本,然后取消选中下面的所有复选框 启用脚本。
这些设置存储在注册表中,也可以使用 VBA 代码进行设置。关键是 HKEY_CURRENT_USER\Software\SAP\SAPGUI Front\SAP 前端 Server\Security\
非常感谢,这是我现在的最终代码:
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
'
我是这样做的,因为我不会是唯一一个person/user使用宏的人,所以我不必告诉每个人更改他们在 SAP 中的设置。
同时感谢:https://www.slipstick.com/developer/read-and-change-a-registry-key-using-vba/