Linking Visio shapes and VBA (class module)

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?
 
J

JuneTheSecond

I think there is no way other than the way you might think.
But there might be ways to look code elegant.
Though I do not know how would you feel, there is a VBA example that is not
for Visio but is for testing framework. My suggestion is VBAUnit written by
Rob Harwood. http://www.rapid-programming.com/vbaunit/
 
V

vojo

given the number of user rows and prop rows supported...your complex
calculation must be one really really complex calcuation. And even if you
did run out of rows and such...you could make the shape a group and do
parts of the calc in shapes (hidden if you like) within that group.

Aside from all that, you can set up the vba call to pass the calling shape
so that you dont have to do alot of searching sorting to find what changed
in the vba code (ie know which sections/rows to go after in which shape
and where to write results)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top