Visio (Documents.ShapeAdded 事件)

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

語法

運算式ShapeAdded (Shape)

表達 代表 Documents 物件的變數。

參數

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

註解

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

SelectionAddedShapeAdded事件很類似,因為它們會在建立圖形 (的) 之後引發。 它們在單一作業新增數個圖形時的行為不同。 假設 貼上 作業會建立三個新圖形。 ShapeAdded事件會引發三次,並針對這三個物件各採取動作。 SelectionAdded事件會引發一次,而且會在選取三個新圖形的Selection物件上作用。

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

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

如果您想要建立自己的 Event 物件,請使用 AddAddAdvise 方法。

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

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

若要尋找您要建立之事件的事件代碼,請參閱 事件代碼

範例

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

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

每當有圖形新增至繪圖頁 (無論該圖形是拖曳自樣板、使用繪圖工具繪製,或是從 [剪貼簿] 貼上) 時,ShapeAdded 事件處理常式就會開始執行。 處理常式會檢查新圖形的 Master 屬性,如果圖形是以 Square 主圖形為基礎,則會遞增 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 支援與意見反應