共用方式為


Shape.ShapeAdded 事件 (Visio)

會在新增一個或多個圖形至文件之後發生。

語法

表情ShapeAdded (Shape)

expression 一個用於表示 Shape 物件的變數。

參數

名稱 必要/選用 資料類型 描述
圖形 必要 [IVSHAPE] 已加入到文件中的圖形或圖形群組。

註解

如果形狀的 Type 屬性是 visTypeGroup (2) 或 visTypePage (1) ,則 Shape 物件可以作為 ShapeAdded 事件的來源物件。

SelectionAddedShapeAdded 事件相似,因為它們都是在 shape (s) 建立後觸發。 當單一操作加入多個形狀時,它們的行為有所不同。 假設一個 Paste 操作會產生三個新形狀。 ShapeAdded 事件會觸發三次,並作用於三個物件中的每個。 SelectionAdded 事件會觸發一次,並作用於選取三個新形狀的 Selection 物件。

要判斷 ShapeAdded 事件是因為新增形狀或一組形狀被加入頁面、一組現有形狀被分組,或是貼上動作觸發,你可以使用 Application.IsInScope 屬性。 如果傳送 visCmdObjectGroupIsInScope 傳回 True,則 ShapeAdded 事件是由群組動作所觸發。 如果 IsInScope 在傳遞 visCmdUFEditPastevisCmdEditPasteSpecial 時回傳 True,則 ShapeAdded 事件是由貼上操作觸發的。 如果傳送所有這些引數時 IsInScope 傳回 False,則該事件必定是由新增至頁面的新圖形所觸發。

如果您是使用 Microsoft Visual Basic 或 Visual Basic for Applications (VBA),則本主題中的語法會描述有效處理事件的常用方式。

如果你想建立自己的 事件 物件,可以使用 AddAddAdvise 方法。

若要建立可以執行附加元件的 Event 物件,可使用 Add 方法,因為它可適用於 EventList 集合。

若要建立用於接收通知的 Event 物件,則使用 AddAdvise 方法。

要找到你想建立的事件的事件代碼,請參見事件代碼。

範例

此 VBA 範例會顯示如何計算新增至繪圖中的圖形數,該繪圖是以稱為 Square 的主圖形為基礎。 把程式碼貼到Visio中該文件的專案中。

儲存使用中文件後,DocumentSaved 事件處理常式就會開始執行。 處理器初始化一個整數變數 intNumberOfSquares ,用來儲存計數。

每當有圖形新增至繪圖頁 (無論該圖形是拖曳自樣板、使用繪圖工具繪製,或是從 [剪貼簿] 貼上) 時,ShapeAdded 事件處理常式就會開始執行。 處理器會檢查新形狀的 屬性,若該形狀基於 方形 主控,則會遞增 intNumberOfSquares

 
Dim intNumberOfSquares As Integer 
 
Private Sub Document_DocumentSaved(ByVal vsoDocument As Visio.IVDocument) 
 
 'Initialize number of squares added. 
 intNumberOfSquares = 0 
 
End Sub 
 
Private Sub Document_ShapeAdded(ByVal vsoShape As Visio.IVShape) 
 
 Dim vsoMaster As Visio.Master 
 
 'Get the Master property of the shape. 
 Set vsoMaster = vsoShape.Master 
 
 'Check whether the shape has a master. If not, 
 'the shape was created locally. 
 If Not (vsoMaster Is Nothing) Then 
 
 'Check whether the master is "Square". 
 If vsoMaster.Name = "Square" Then 
 
 'Increment the count for the number of squares added. 
 intNumberOfSquares = intNumberOfSquares + 1 
 
 End If 
 
 End If 
 
 MsgBox "Number of squares: " & intNumberOfSquares, vbInformation, _ 
 "Document Created Example" 
 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應