如何处理等待另一个应用程序 (SAP) 完成 OLE 操作?

How can I handle waiting for another application (SAP) to complete an OLE action?

我正在尝试在 SAP 中自动化流程。

我把一系列程序录了下来,把代码贴在一个宏里sheet。我使用以下命令让 Excel 读取 VBA 代码:

Dim SapGuiAuto As Object
Dim Application As Object
Dim Connection As Object
Dim Session As Object

Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
Set Connection = Application.Children(0)
Set Session = Connection.Children(0)

对于其中一个部分,SAP 进行了大量计算,这需要几分钟时间,在计算过程中,Excel 生成消息:

我必须点击确定才能继续,然后它会不停地弹出,我必须这样做 10-15 次,这就消除了自动化的意义。

在没有宏的情况下,SAP 没有给我任何错误。

我尝试用

关闭它
Application.DisplayAlerts = False

它反而给了我:

我搜索了网络和网站,但几乎没有任何有用的信息,可能是因为我的问题太具体了。

一些附加信息:
1. 我的笔记本电脑是工作的,未经许可我无法安装任何其他软件或更新。
2. 我尝试检查 DDE 的 Excel 选项,但是当我尝试 运行 脚本时它只会给我一个错误。

我的 Excel 版本是 2013.

如何实现我能想到的三种解决方案之一:
1. 禁用 OLE
的弹出窗口 2.让它每次出现都自动点击OK
3. 让 Excel 冻结并等待 SAP 执行它的操作? (不知道这是否有意义。)尝试使用 Application.Wait,但没有成功

上面的 Storax 实际上给了另一个题目一个 link 和问题的答案。

代码在这里:

Private Declare Function _
    CoRegisterMessageFilter Lib "OLE32.DLL" _
    (ByVal lFilterIn As Long, _
    ByRef lPreviousFilter) As Long

Sub KillMessageFilter()  
    '''Original script Rob Bovey  

    '''https://groups.google.com/forum/?hl=en#!msg/microsoft.public.excel.programming/ct8NRT-o7rs/jawi42S8Ci0J
    '''http://www.appspro.com/

    Dim lMsgFilter As Long

    ''' Remove the message filter before calling Reflections.
    CoRegisterMessageFilter 0&, lMsgFilter

    ''' Call your code here....

    ''' Restore the message filter after calling Reflections.
    CoRegisterMessageFilter lMsgFilter, lMsgFilter

End Sub

如果您使用 64 位 Microsoft Office,您应该根据 Declare Statement 稍微更改 M_Delineshev 提供的代码。尝试使用这个版本:

Private Declare PtrSafe Function _
    CoRegisterMessageFilter Lib "OLE32.DLL" _
    (ByVal lFilterIn As Long, _
    ByRef lPreviousFilter) As LongPtr


Sub KillMessageFilter() '''Original script Rob Bovey

'''https://groups.google.com/forum/?hl=en#!msg/microsoft.public.excel.programming/ct8NRT-o7rs/jawi42S8Ci0J '''http://www.appspro.com/

Dim lMsgFilter As Long

''' Remove the message filter before calling Reflections. CoRegisterMessageFilter 0&, lMsgFilter

''' Call your code here....

''' Restore the message filter after calling Reflections. CoRegisterMessageFilter lMsgFilter, lMsgFilter

End Sub

以下代码对我有用...

Private Declare PtrSafe Function CoRegisterMessageFilter Lib "OLE32.DLL" (ByVal lFilterIn As Long, ByRef lPreviousFilter) As Long

Sub IgnoreMessages()

Dim lMsgFilter As Long

CoRegisterMessageFilter 0&, lMsgFilter

'Write your code here

CoRegisterMessageFilter lMsgFilter, lMsgFilter

End Sub