3 4
If your solution maintains any type of parallel model that contains data associated with the shapes in your drawing, it is important that the model correctly represents the state of the drawing.
To ensure that your drawings and models remain synchronized, you can add undo units to the Microsoft Visio Undo manager. An undo unit is a Component Object Model (COM) object that supports the Visio IVBUndoUnit interface (or the IOleUndoUnit interface on an in-process COM object). It contains the actions needed to undo or redo changes to an external data source that reflect changes in the drawing.
The Visio IVBUndoUnit interface is designed to allow programmers using Microsoft Visual Basic for Applications (VBA) and Microsoft Visual Basic to create their own undo units for the Visio Undo manager. To create an undo unit, you must implement this interface, along with all of its public procedures, in a class module that you insert into your project. This section describes creating an undo unit by implementing the IVBUndoUnit interface in a VBA project.
For details about implementing interfaces in VBA or Visual Basic, search for the Implements keyword in your Visual Basic documentation. For details about IVBUndoUnit, see the Visio type library.
You can take two approaches to creating an undo unit:
This topic and the next, Creating an Undo Unit that Maintains Non-Visio Data: an Example, describe a single undo unit that maintains its undo/redo state.
Implements Visio.IVBUndoUnit
Inside this class module you must implement all of the public procedures of IVBUndoUnit. After you code the Implements statement, IVBUndoUnit will appear in the Object box in the code window.
Skeleton procedures become available in the Procedure box for each of the procedures you must implement, as described in the following table.
IVBUndoUnit procedures
Procedure name | Description |
Description | This property returns a string describing the undo unit. This string appears in the Undo list on the Standard toolbar. |
Do | This method provides the actions that are required to undo and redo your action. If you are creating a single object for undoing and redoing, this procedure maintains your undo/redo state and adds an undo unit to the opposite stack. If the Do method is called with a Nothing pointer, the unit should carry out the undo action but should not place anything on the undo or redo stack. |
OnNextAdd | This method gets called when the next undo unit in the same scope gets added to the undo stack. When an undo unit receives an OnNextAdd notification, it communicates back to the creating object that it can longer insert data into this undo unit. |
UnitSize | This property returns the approximate memory size (in bytes) of the undo unit plus resources it has allocated. The Visio engine may use this memory size when deciding whether to purge the undo queue. This value is optional and can be zero (0). |
UnitTypeCLSID | This property returns a string value that can contain a CLSID to identify your undo units. You can use the same CLSID for multiple undo units and use different values in the UnitTypeLong procedure. This value is optional and can be a Null or empty string. |
UnitTypeLong | This property returns a Long, which you can use to identify your objects. This value is optional and can be 0. |
At any location in your program where you perform an action in your parallel model, you should add an instance of your undo unit that can undo or redo your action. In this way, it will get called along with everything else in the undo stack if your add-on's changes get undone.
For example, if the class module that you inserted into the project was named MyUndoUnit :
Application.AddUndoUnit New MyUndoUnit
For details about the New keyword, see your Visual Basic documentation.