以下代码供参考,关键是在需要屏蔽事件时,释放Application事件对象变量(=Nothing) 请注意预期事件和预期动作的代码前后次序 '* +++++++++++++++++++++++++++++ '* Created By SHOUROU@ExcelHome 2006-7-18 5:22:59 '仅测试于System: Windows NT Word: 11.0 Language: 2052 '№ 0038^The Code CopyIn [ThisDocument-ThisDocument]^' '* ----------------------------- Public WithEvents myApp As Word.Application '声明Application事件的对象变量 Private Sub Document_Open() Set myApp = Word.Application '初始化声明的对象 End Sub '---------------------- Private Sub myApp_WindowSelectionChange(ByVal Sel As Selection) MsgBox Sel.Text '每次移动光标或者所选内容时输出其内容 End Sub '---------------------- Sub Example() '测试禁用Application对象事件 With Selection Set myApp = Nothing '释放对象 .HomeKey wdStory '移到光标到文档首位置,此时,将不再触发WindowSelectionChange事件 .WholeStory '全选文档,此操作为预期事件,即结合下面代码将导致触发WindowSelectionChange事件 Set myApp = Word.Application '初始化声明的对象,注意它应在预期动作之后 End With End Sub |