Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

sap-gui

I am trying to automate a process in SAP GUI.

I recorded a series of procedures and pasted the code in a macro sheet. I make Excel read the VBA code with the following commands:

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)

For one of the parts SAP makes a lot of calculations which takes a few minutes and around the middle of it, Excel generates the message:
enter image description here

I have to click OK to continue, then it pops-up non-stop and I have to do it like 10-15 times, which removes the point of automation.

When doing it without a macro, SAP does not give me any error.

I tried turning it off with

Application.DisplayAlerts = False

It instead gives me:

enter image description here

I searched the net and the site, but there was barely any useful info maybe because my question is too specific.

Some additional info:

  1. My laptop is from work and I cannot install any additional software or updates to it without permission.
  2. I tried checking the Excel option for DDE, but it only gives me an error when I try to run the script.
    enter image description here

My Excel version is 2013.

How can I achieve one of the three solutions I can think of:

  1. Disable the pop up for OLE
  2. Make it automatically click OK every time it appears
  3. Make Excel freeze and wait while SAP does its thing? (Don't know if that makes sense.) Tried with Application.Wait, but without success
like image 513
M_Delineshev Avatar asked Oct 28 '25 09:10

M_Delineshev


2 Answers

Storax above actually gave a link to another topic with the solution of the question.

Code here:

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
like image 87
M_Delineshev Avatar answered Oct 31 '25 06:10

M_Delineshev


If you use 64-bit Microsoft Office you should slightly change the code offered by M_Delineshev according to the Declare Statement. Try to use this version:

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
like image 31
a_babich Avatar answered Oct 31 '25 07:10

a_babich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!