【问题标题】:How can I handle waiting for another application (SAP) to complete an OLE action?如何处理等待另一个应用程序 (SAP) 完成 OLE 操作?
【发布时间】:2018-05-02 19:22:47
【问题描述】:

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

我记录了一系列程序并将代码粘贴在宏表中。我使用以下命令让 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.让它每次出现时自动点击确定
3.让Excel冻结并等待SAP做它的事情? (不知道这是否有意义。)尝试使用Application.Wait,但没有成功

【问题讨论】:

  • 我认为您必须向我们提供您在 SAP 中做什么的信息。您显示的代码部分没问题。我没有看到任何问题。
  • 嗨,我的代码没有问题,我在其他事务和操作中使用了类似的代码,效果很好。问题出在 SAP 或 Excel 上,因为 SAP 完成操作所需的时间太长,并且 Exce 给出了我截屏的错误。那么我怎样才能让 Excel 不等待 OLE...
  • 那么也许this 有帮助
  • 谢谢斯托拉克斯!

标签: excel vba sap


【解决方案1】:

上面的Storax实际上给出了另一个主题的链接以及问题的解决方案。

代码在这里:

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

【讨论】:

    【解决方案2】:

    如果您使用 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
    

    【讨论】:

    • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和文档链接。如果没有围绕它的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现 how to write a good answer 非常有帮助。请编辑您的答案。
    • 将此代码粘贴到哪里?
    • 在模块中还是在工作簿的查看代码区?
    【解决方案3】:

    以下代码对我有用...

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-30
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多