Home/ Reference/ Events

app.AdviseEvents(...) / UnadviseEvents(...)

Event hooks are a VBA feature. Reaching them from Python over COM effectively doesn't work (it needs an IDispatch event sink), so don't burn time on it — write the l

Signature

app.AdviseEvents(...) / UnadviseEvents(...)

Notes

Event hooks are a VBA feature. Reaching them from Python over COM effectively doesn't work (it needs an IDispatch event sink), so don't burn time on it — write the listener as a VBA macro and let Python handle the part being called.

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
# Python usually cannot get these callbacks; listed for completeness only
try:
    print("Event API is present: ", bool(app.AdviseEvents))
except Exception as e:
    print("API unavailable: ", e)

# the Python alternative: poll with a delay
import time
last = set()
layer = doc.ActivePage.ActiveLayer
for _ in range(5):
    now = set()
    for i in range(1, layer.Shapes.Count + 1):
        now.add(layer.Shapes.Item(i).StaticID)
    added = now - last
    if added:
        print("appeared", len(added), " object(s)")
    last = now
    time.sleep(1)

About this section: Events

Events — waiting for user interaction.

Other entries in this section