首页/ 接口索引/ 交互与事件

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

VBA 里挂事件监听用。Python 通过 COM 调用这条路基本走不通(需要实现 IDispatch 事件接口),别在这上面浪费时间——要监听就写 VBA 宏,Python 负责被调用的那部分。

签名

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

说明

VBA 里挂事件监听用。Python 通过 COM 调用这条路基本走不通(需要实现 IDispatch 事件接口),别在这上面浪费时间——要监听就写 VBA 宏,Python 负责被调用的那部分。

Python 示例(可直接运行)

依赖 pywin32。示例假定 app / doc / page / layer 已就绪。 见「连接与初始化」
Python
# 这段在 Python 里通常拿不到回调,只做存在性说明
try:
    print("事件监听接口存在:", bool(app.AdviseEvents))
except Exception as e:
    print("该接口不可用:", e)

# Python 的替代方案:轮询 + 间隔判断
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("新出现", len(added), "个对象")
    last = now
    time.sleep(1)

关于本类:交互与事件

有两个接口能让脚本"停下来等用户操作",做半自动工具特别顺手:脚本干粗活,关键位置让人点一下。

同类其他接口