K
kiz_0987
I am using class modules to programmatically generate Visio shapes in
VBA. Ideally I wanted these shapes to inherit from a lower level class
but I know this is not possible in VBA so I have used simulated
inheritance by creating an object of the lower level class in the
shape. One reason (of many) for me to do this is to allow me to reuse
the VBA code for the lower level object in other applications (eg
Excel) without the need for Visio.
Example:
Class module: Car
Public Property Get ComplicatedCalculation
Here is a complex calculation - too complex to really include in the
shape cells.
End Property
Class module: Car_Shape
Public Sub Class_Initialize()
Set m_Car = New Car
End Sub
Public Property Get ComplicatedCalculation
Here is a complex calculation - too complex to really include in the
shape cells.
ComplicatedCalculation = m_Car.ComplicatedCalculation
End Property
The 'ComplicatedCalculation' uses parameters which are stored as
part of the shape (User and Prop cells), as well as the shapes
location, angle to other shapes etc and stores the result in another
User cell. When these parameters are changed (eg moving the shape), an
event is called and the required user cell is recalculated and stored
again. Not ideal, but the calculation is quite complex and not easy to
generate using Visio formulae. I can get this all to work, but in an
ugly way (which continually creates and destroys objects):
- Find out which shape changed = SelectedShape (get the event to
work).
- Check if SelectedShape is a Car_Shape (checking part of the NameID)
- Generate an instance of Car_Shape (which will have default values)
and rewrite the User/Prop cells with the values from SelectedShape
(using a child function of Car_Shape - GetCar_ShapeFromShape).
- Call the ComplicatedCalculation (calls Car:: ComplicatedCalculation)
and put the result in the required SelectedShape User cell
- Destroy the Car_Shape and Car object (assumed to happen
automatically). SelectedShape still exists.
My question is - is there a more elegant way to create this linking
between a VBA class module and a Visio shape?
VBA. Ideally I wanted these shapes to inherit from a lower level class
but I know this is not possible in VBA so I have used simulated
inheritance by creating an object of the lower level class in the
shape. One reason (of many) for me to do this is to allow me to reuse
the VBA code for the lower level object in other applications (eg
Excel) without the need for Visio.
Example:
Class module: Car
Public Property Get ComplicatedCalculation
Here is a complex calculation - too complex to really include in the
shape cells.
End Property
Class module: Car_Shape
Public Sub Class_Initialize()
Set m_Car = New Car
End Sub
Public Property Get ComplicatedCalculation
Here is a complex calculation - too complex to really include in the
shape cells.
ComplicatedCalculation = m_Car.ComplicatedCalculation
End Property
The 'ComplicatedCalculation' uses parameters which are stored as
part of the shape (User and Prop cells), as well as the shapes
location, angle to other shapes etc and stores the result in another
User cell. When these parameters are changed (eg moving the shape), an
event is called and the required user cell is recalculated and stored
again. Not ideal, but the calculation is quite complex and not easy to
generate using Visio formulae. I can get this all to work, but in an
ugly way (which continually creates and destroys objects):
- Find out which shape changed = SelectedShape (get the event to
work).
- Check if SelectedShape is a Car_Shape (checking part of the NameID)
- Generate an instance of Car_Shape (which will have default values)
and rewrite the User/Prop cells with the values from SelectedShape
(using a child function of Car_Shape - GetCar_ShapeFromShape).
- Call the ComplicatedCalculation (calls Car:: ComplicatedCalculation)
and put the result in the required SelectedShape User cell
- Destroy the Car_Shape and Car object (assumed to happen
automatically). SelectedShape still exists.
My question is - is there a more elegant way to create this linking
between a VBA class module and a Visio shape?