EventList 方法 (Visio)EventList.Add method (Visio)

添加在事件发生时运行加载项的Event对象。Adds an Event object that runs an add-on when an event occurs. Event对象将添加到要接收其事件的源对象的EventList集合中。The Event object is added to the EventList collection of the source object whose events you want to receive.


表达式(EventCodeActionTargetTargetArgs)expression.Add (EventCode, Action, Target, TargetArgs)

_表达式_一个代表**EventList** 对象的变量。expression A variable that represents an EventList object.


名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
EventCodeEventCode 必需Required IntegerInteger 要捕获的事件。The event(s) to capture.
ActionAction 必需Required IntegerInteger 要执行的操作。The action to perform. 必须为visActCodeRunAddon, 即 Visio 类型库中**VisEventCodes** 的成员。Must be visActCodeRunAddon, a member of VisEventCodes in the Visio type Library.
TargetTarget 必需Required StringString 加载项的名称。The name of your add-on.
TargetArgsTargetArgs 必需Required StringString 传递给Event对象以设置其TargetArgs属性的字符串。The string that is passed to your Event object to set its TargetArgs property.

返回值Return value



源对象, 其EventList集合包含Event对象, 用于建立报告事件的范围。The source object whose EventList collection contains the Event object establishes the scope in which the events are reported. 为源对象和对象模型层次结构中较低的对象报告事件。Events are reported for the source object and objects lower in the object model hierarchy. 例如,要在打开特定文档时运行加载项,请将 DocumentOpened 事件的 Event 对象添加到该文档的 EventList 集合。For example, to run an add-on when a particular document is opened, add an Event object for the DocumentOpened event to the EventList collection of that document. 要在应用程序实例中打开任意文档时运行加载项,请将 Event 对象添加到 Application 对象的 EventList 集合中。To run an add-on when any document is opened in an instance of the application, add the Event object to the EventList collection of the Application object.

创建事件对象是处理 c + + 或其他非 Microsoft Visual Basic 解决方案中的事件的常用方法。Creating Event objects is a common way to handle events from C++ or other non-Microsoft Visual Basic solutions. 使用 Visual Basic WithEvents关键字处理事件时, 会触发源对象的事件集中的所有事件, 但在创建事件对象时, 将只会通知您所选择的事件的程序。When you use the Visual Basic WithEvents keyword to handle events, all the events in a source object's event set fire, but when you create Event objects, your program will only be notified of the events you select. 根据您的解决方案,这可能会提高性能。Depending on your solution, this may result in improved performance.

运行加载项的 Event 对象可以是永久性的:即它们可与 Visio 文档一起存储。Event objects that run add-ons can be persistent: that is, they can be stored with a Visio document. 要具有永久性,Event 对象的 PersistentPersistable 属性必须都为 TrueTo be persistent, an Event object's Persistent and Persistable properties must both be True.

传递给Add方法的参数设置Event对象的eventAction (visActCodeRunAddon)、 TargetTargetArgs属性的初始值。The arguments passed to the Add method set the initial values of the Event object's Event, Action (visActCodeRunAddon), Target, and TargetArgs properties.

事件代码由 Visio 类型库声明并具有前缀visEvtEvent codes are declared by the Visio type library and have the prefix visEvt. 事件代码通常为常量的组合。Event codes are often a combination of constants. 例如, visEvtAdd + visEvtDocDocumentAdded事件的事件代码。For example, visEvtAdd + visEvtDoc is the event code for the DocumentAdded event. 若要查找要创建的事件的事件代码,请参阅事件代码To find an event code for the event that you want to create, see Event Codes.

若要创建一个事件对象来建议调用方的接收器对象有关事件, 请参阅AddAdvise方法。To create an Event object that advises the caller's sink object about an event, see the AddAdvise method.


下面的示例演示如何将运行加载项的Event对象添加到源对象的EventList集合, 在此示例中, 要接收其事件的文档对象。The following example shows how to add an Event object that runs an add-on to the EventList collection of the source object, in this case a Document object, whose events you want to receive.

在运行此宏之前, 请将_路径_ \ _文件名_替换为 Visio 项目中的可执行加载项 (EXE) 的有效路径和文件名。Before running this macro, replace path \ filename with a valid path and file name for an executable add-on (EXE) in your Visio project. 该加载项应该不使用任何参数。The add-on should take no arguments.

Public Sub AddEvent_Example() 
 Dim vsoAddons As Visio.Addons 
 Dim vsoEventList As Visio.EventList 
 Dim vsoDocument As Visio.Document 
 Dim vsoEvent As Visio.Event 
 Dim vsoAddon As Visio.Addon 
 'Add a document based on the Basic Diagram template. 
 Set vsoDocument = Documents.Add("Basic Diagram.vst") 
 'Add an add-on to the Addons collection. 
 Set vsoAddons = Visio.Addons 
 Set vsoAddon = vsoAddons.Add("path \filename ") 
 'Add a BeforeDeleteSelection event to the EventList collection 
 'of the Document object. The event will start your add-on, 
 'which takes no arguments. 
 Set vsoEventList = vsoDocument.EventList 
 Set vsoEvent = vsoEventList.Add(visEvtCodeBefSelDel, _ 
 visActCodeRunAddon, _ 
 "path \filename ", "") 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.