Visio handling of events

G

gnicholls

Hi,

I know this is a real novice question, but could anybody tell me how t
run a piece of code based on a Visio event. I'm playing around with th
following code :-

Private Sub Command1_ShapeAdded()
Dim shp As Visio.Shape
Dim S As String
Set shp = Visio.ActiveWindow.Selection.Item(1)
S = "Shape Name = " & shp.Name
S = S & Chr(13)
S = S & "Shape Area = " & shp.AreaIU & " Square Inches."
MsgBox S
End Sub

As long as I add a shape to a visio document I can run this code fro
within the VB editor and get the results that I want. However eithe
I'm missing the point or missing a step, but I want this code to ru
from within the Visio document simply when a shape is added (I don'
want to have to run it from the VB editor or have to select a Macro t
run).

Just to push my luck, if anyone knows the answer to the above woul
they be able to indicate with the above example how to make the cod
run based on the Click event so that if I have one shape on the activ
document and then click on it the message box is then presented.

Any help would be appreciated
 
A

Al Edlund

many events can fire from what appears to be a single trigger to the user.
Your description of what you would like to do actually touches on two of
them specifically, the shape created and shape selected events. here's a
pointer to get you started.
http://msdn.microsoft.com/library/en-us/devref/HTML/DVS_21_Event_Handling_857.asp?frame=true

you might consider adding this code to experiment with

' a little bit of overkill
Private WithEvents pagObj As Visio.Page
Private WithEvents appObj As Visio.Application
Private WithEvents shpObj As Visio.Shape
Private WithEvents winObj As Visio.Window
Private WithEvents docObj As Visio.Document

dim s as string

Private Sub winObj_SelectionChanged(ByVal Window As IVWindow)
If ActiveWindow.Selection.Count > 0 Then
'Debug.Print ActiveWindow.Selection.Count & " devices selected"
For intObjCount = 1 To ActiveWindow.Selection.Count
Set objShape = ActiveWindow.Selection.item(intObjCount)
S = "Shape Name = " & objshape.Name
S = S & Chr(13)
S = S & "Shape Area = " & objshape.AreaIU & " Square Inches."
MsgBox S
next
end if
end sub
 
G

gnicholls

I think that part of my question is even more fundamental. When I try to
run the code that you kindly supplied me with, Visio prompts me to enter
a Macro name. I enter the name and then nothing happens (although the
macro is available via the Macro menu option). To further test this I
took a complete working Visio diagram from the internet and deleted all
of the VB code sitting behind it, ran through a "Compile" from the
"Debug" menu and just to make sure carried out a "Save". I then found
that the Visio document still runs with all of its functionality in
place. What am I not doing correctly?

Many thanks.
 
A

Al Edlund

I apologize, I had not included the initialization
add this routine to the document and call it via the macro toolbar. Then
when you select a shape on the page you should get your messages for
testing.
al

Public Sub Init()
Set appObj = Visio.Application
Set winObj = appObj.ActiveWindow
End Sub
 

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