Documents.ShapeAdded 事件 (Visio)

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

語法

運算式ShapeAdded (Shape)

運算式 代表 Documents 物件的變數。

參數

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

註解

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

SelectionAddedShapeAdded 事件的相似之處在於,它們都會在圖形 () 建立後觸發。 當單一作業新增數個圖形時,它們的行為方式不同。 假設 [貼上 ] 作業建立三個新圖形。 ShapeAdded 事件會觸發三次,並分別處理三個物件。 SelectionAdded 事件會觸發一次,並作用於已選取三個新圖形的 Selection 物件。

若要判斷 ShapeAdded 事件是由新圖案觸發或是被新增到頁面的圖形群組、將一組現有圖形組成群組,還是由貼上動作觸發,您可以使用 Application.IsInScope 屬性。 如果傳送 visCmdObjectGroupIsInScope 傳回 True,則 ShapeAdded 事件是由群組動作所觸發。 如果 IsInScope 在傳遞 visCmdUFEditPastevisCmdEditPasteSpecial 時傳回 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 master 為基礎,則遞增 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 支援與意見反應