Visio) (Document.BeginUndoScope 方法

为 Microsoft Visio 实例启动具有唯一范围 ID 的事务。

语法

表达式。BeginUndoScope (bstrUndoScopeName)

表达式 表示 Document 对象的变量。

参数

名称 必需/可选 数据类型 说明
bstrUndoScopeName 必需 String 范围的名称;可能出现在 Visio 用户界面中。

返回值

Long

备注

如果需要了解收到的事件是否为您启动的特定操作的结果,请使用 BeginUndoScope 和 EndUndoScope 方法封装您的操作。 在事件处理程序中,使用 IsInScope 属性来测试 BeginUndoScope 方法返回的范围 ID 是否为当前上下文的一部分。 当您收到具有该 ID 的 ExitScope 事件时,请确保从 BeginUndoScope 属性中清除存储的范围 ID。

必须使对 BeginUndoScope 方法和 EndUndoScope 方法的调用实现平衡。 如果调用的是 BeginUndoScope 方法,则在完成构成您的范围的操作后,应该立即调用 EndUndoScope 方法。 另外,尽管对多个文档的操作在单个范围中应该是可靠的,但是关闭文档可能会产生副作用,它会清除当前打开的范围的撤消信息并清除撤消和恢复堆栈。 如果发生这种情况,将 bCommit = False 传递到 EndUndoScope 不会还原撤消信息。

也可以使用 BeginUndoScope 和 EndUndoScope 方法将由加载项定义的动作添加到 Visio 撤消流。 当您在无模式方案(其中初始化代理为加载项用户界面或无模式编程动作的一部分)中进行操作时,这很有用。

注意

大多数 Visio 操作已包装在内部撤消作用域中,因此在应用程序中运行的加载项不需要调用此方法。

示例

以下示例显示如何使用 BeginUndoScope 方法为 Visio 实例启动具有唯一范围 ID 的事务。

 
Private WithEvents vsoApplication As Visio.Application 
Private lngScopeID As Long 
 
Public Sub BeginUndoScope_Example() 
 
 Dim vsoShape As Visio.Shape 
 
 'Set the module-level application variable to 
 'trap application-level events. 
 Set vsoApplication = Application 
 
 'Begin a scope and set the module-level scope ID variable. 
 lngScopeID = Application.BeginUndoScope("Draw Shapes") 
 
 'Draw three shapes. 
 Set vsoShape = ActivePage.DrawRectangle(1, 2, 2, 1) 
 ActivePage.DrawOval 3, 4, 4, 3 
 ActivePage.DrawLine 4, 5, 5, 4 
 
 'Change a cell to trigger the CellChanged event. 
 vsoShape.Cells("Width").Formula = 5 
 
 'End and commit this scope. 
 Application.EndUndoScope lngScopeID, True 
 
 End Sub 
 
 Private Sub vsoApplication_CellChanged(ByVal Cell As IVCell) 
 
 'Check to see if this cell change is the result of something 
 'happening within the scope. 
 If vsoApplication.IsInScope(lngScopeID) Then 
 Debug.Print Cell.Name & " changed in scope "; lngScopeID 
 End If 
 
End Sub 
 
Private Sub vsoApplication_EnterScope(ByVal app As IVApplication, _ 
 ByVal nScopeID As Long, _ 
 ByVal bstrDescription As String) 
 
 If vsoApplication.CurrentScope = lngScopeID Then 
 Debug.Print "Entering my scope " & nScopeID 
 Else 
 Debug.Print "Enter Scope " & bstrDescription & "(" & nScopeID & ")" 
 End If 
 
End Sub 
 
Private Sub vsoApplication_ExitScope(ByVal app As IVApplication, _ 
 ByVal nScopeID As Long, _ 
 ByVal bstrDescription As String, _ 
 ByVal bErrOrCancelled As Boolean) 
 
 If vsoApplication.CurrentScope = lngScopeID Then 
 Debug.Print "Exiting my scope " & nScopeID 
 Else 
 Debug.Print "Exit Scope " & bstrDescription & "(" & nScopeID & ")" 
 End If 
 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。