L
Leigh
I have a very cool macro that adds connections to an Ethernet shape.
The full code is included below. It works wonderfully -- take a minute
to copy the code into a Visio document, drag an Ethernet shape (from
the Basic Network Shapes stencil) onto the page, select the shape, then
run the macro. Oooooohhhh.
Now here's what I *can't* figure out. How can I associate the macro
with the shape's double-click event? I have tried saving the macro in a
module contained within the stencil file itself. But if I do, the
macro doesn't appear on the drop-down list under Format, Behavior,
Double-Click, Run macro.... I can manually copy the module into the
Visio drawing I'm working on (the one where I drop the shape), but
that's no solution. My fantasy: whenever I use the stencil and drag an
Ethernet shape onto *any* drawing, I want to double-click it and have
my nifty macro run.
OK Vizards -- how can I do that?
Here's the code:
Sub EthernetAddConnection()
Dim loEthernetShape As Visio.Shape
Dim loApp As Visio.Application
Dim loPage As Visio.Page
Dim loControlsSection As Visio.Section
Dim liGeometrySection As Integer
Dim liNewControl As Integer
Dim liCell As Integer
'Set loApp = Application
Set loEthernetShape = Application.ActiveWindow.Selection.Item(1)
If loEthernetShape.NameU <> "Ethernet" Then
MsgBox "Select an Ethernet shape, then try again",
vbExclamation, "Error: not an Ethernet shape"
Exit Sub
End If
'Get the Controls section
Set loControlsSection = loEthernetShape.Section(visSectionControls)
'Get a reference to the last existing control
Dim loExistingRow As Visio.Row
Set loExistingRow = loControlsSection.Row(loControlsSection.Count -
1)
'Add a new control
liNewControl = loExistingRow.Index + 1
loEthernetShape.AddRow visSectionControls, liNewControl, 0
'Set its cell formulae
'Get a ref to the row we just added
Dim loNewRow As Visio.Row
Set loNewRow = loControlsSection.Row(loExistingRow.Index + 1)
'Set the formulas so that this control will wake up just to
'the right and below the Ethernet shape
loNewRow.Cell(0).Formula = "Width*1.1"
loNewRow.Cell(1).Formula = "Height*-1.25"
loNewRow.Cell(2).Formula = "Controls.X" & liNewControl + 1
loNewRow.Cell(3).Formula = "Controls.YDyn"
loNewRow.Cell(4).Formula = "0"
loNewRow.Cell(5).Formula = "0"
loNewRow.Cell(6).Formula = "TRUE"
loNewRow.Cell(7).Formula = "Controls.Prompt"
'Add a new geometry section.
liGeometrySection =
loEthernetShape.AddSection(visSectionFirstComponent)
Dim loNewGeometrySection As Visio.Section
Set loNewGeometrySection =
loEthernetShape.Section(liGeometrySection)
Dim lsSectionName As String
'Insert its component row
loEthernetShape.AddRow liGeometrySection, visRowFirst + 0,
visTagComponent
Dim loThisRow As Visio.Row
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 0)
lsSectionName = "Geometry1"
'Set the formulas for the component row
loThisRow.Cell(0).Formula = "TRUE"
loThisRow.Cell(1).Formula = ""
loThisRow.Cell(2).Formula = "FALSE"
loThisRow.Cell(3).Formula = ""
'Add three rows for the geometry details
'First, the MoveTo
loEthernetShape.AddRow liGeometrySection, visRowFirst + 1,
visTagMoveTo
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 1)
'Enter the formulas
loThisRow.Cell(0).Formula = "Controls.X" & liNewControl + 1
loThisRow.Cell(1).Formula = "Controls.Y" & liNewControl + 1
'Next, a LineTo
loEthernetShape.AddRow liGeometrySection, visRowFirst + 2,
visTagLineTo
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 2)
'Enter the formulas
loThisRow.Cell(0).Formula = lsSectionName & ".X1"
loThisRow.Cell(1).Formula = "Height/2"
'Finally, another LineTo
loEthernetShape.AddRow liGeometrySection, visRowFirst + 3,
visTagLineTo
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 3)
'Enter the formulas
loThisRow.Cell(0).Formula = "Width/2"
loThisRow.Cell(1).Formula = lsSectionName & ".Y2"
End Sub
The full code is included below. It works wonderfully -- take a minute
to copy the code into a Visio document, drag an Ethernet shape (from
the Basic Network Shapes stencil) onto the page, select the shape, then
run the macro. Oooooohhhh.
Now here's what I *can't* figure out. How can I associate the macro
with the shape's double-click event? I have tried saving the macro in a
module contained within the stencil file itself. But if I do, the
macro doesn't appear on the drop-down list under Format, Behavior,
Double-Click, Run macro.... I can manually copy the module into the
Visio drawing I'm working on (the one where I drop the shape), but
that's no solution. My fantasy: whenever I use the stencil and drag an
Ethernet shape onto *any* drawing, I want to double-click it and have
my nifty macro run.
OK Vizards -- how can I do that?
Here's the code:
Sub EthernetAddConnection()
Dim loEthernetShape As Visio.Shape
Dim loApp As Visio.Application
Dim loPage As Visio.Page
Dim loControlsSection As Visio.Section
Dim liGeometrySection As Integer
Dim liNewControl As Integer
Dim liCell As Integer
'Set loApp = Application
Set loEthernetShape = Application.ActiveWindow.Selection.Item(1)
If loEthernetShape.NameU <> "Ethernet" Then
MsgBox "Select an Ethernet shape, then try again",
vbExclamation, "Error: not an Ethernet shape"
Exit Sub
End If
'Get the Controls section
Set loControlsSection = loEthernetShape.Section(visSectionControls)
'Get a reference to the last existing control
Dim loExistingRow As Visio.Row
Set loExistingRow = loControlsSection.Row(loControlsSection.Count -
1)
'Add a new control
liNewControl = loExistingRow.Index + 1
loEthernetShape.AddRow visSectionControls, liNewControl, 0
'Set its cell formulae
'Get a ref to the row we just added
Dim loNewRow As Visio.Row
Set loNewRow = loControlsSection.Row(loExistingRow.Index + 1)
'Set the formulas so that this control will wake up just to
'the right and below the Ethernet shape
loNewRow.Cell(0).Formula = "Width*1.1"
loNewRow.Cell(1).Formula = "Height*-1.25"
loNewRow.Cell(2).Formula = "Controls.X" & liNewControl + 1
loNewRow.Cell(3).Formula = "Controls.YDyn"
loNewRow.Cell(4).Formula = "0"
loNewRow.Cell(5).Formula = "0"
loNewRow.Cell(6).Formula = "TRUE"
loNewRow.Cell(7).Formula = "Controls.Prompt"
'Add a new geometry section.
liGeometrySection =
loEthernetShape.AddSection(visSectionFirstComponent)
Dim loNewGeometrySection As Visio.Section
Set loNewGeometrySection =
loEthernetShape.Section(liGeometrySection)
Dim lsSectionName As String
'Insert its component row
loEthernetShape.AddRow liGeometrySection, visRowFirst + 0,
visTagComponent
Dim loThisRow As Visio.Row
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 0)
lsSectionName = "Geometry1"
'Set the formulas for the component row
loThisRow.Cell(0).Formula = "TRUE"
loThisRow.Cell(1).Formula = ""
loThisRow.Cell(2).Formula = "FALSE"
loThisRow.Cell(3).Formula = ""
'Add three rows for the geometry details
'First, the MoveTo
loEthernetShape.AddRow liGeometrySection, visRowFirst + 1,
visTagMoveTo
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 1)
'Enter the formulas
loThisRow.Cell(0).Formula = "Controls.X" & liNewControl + 1
loThisRow.Cell(1).Formula = "Controls.Y" & liNewControl + 1
'Next, a LineTo
loEthernetShape.AddRow liGeometrySection, visRowFirst + 2,
visTagLineTo
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 2)
'Enter the formulas
loThisRow.Cell(0).Formula = lsSectionName & ".X1"
loThisRow.Cell(1).Formula = "Height/2"
'Finally, another LineTo
loEthernetShape.AddRow liGeometrySection, visRowFirst + 3,
visTagLineTo
Set loThisRow = loNewGeometrySection.Row(visRowFirst + 3)
'Enter the formulas
loThisRow.Cell(0).Formula = "Width/2"
loThisRow.Cell(1).Formula = lsSectionName & ".Y2"
End Sub