Objects within Groups

V

vespasiandamascus

Hi

I want to write a macro that has the capability of taking a selected
group, and highlighting every connection and shape within the group +
their connections. I know how to find connections for a selected
object, but can anyone tell me how to find objects within a group in
VBA?
 
P

Paul Herber

Hi

I want to write a macro that has the capability of taking a selected
group, and highlighting every connection and shape within the group +
their connections. I know how to find connections for a selected
object, but can anyone tell me how to find objects within a group in
VBA?

Do you mean a selection or a group? They are different.

For a selection check ActiveWindow.Selection.Count to see how many
shapes are selected.
For a group, if the main shape is shpObj then look at
shpObj.Shapes.Count to see how many sub-shapes there are.
 
V

vespasiandamascus

Hi Paul.

I would like to select a group, and then run a macro that hides
everything in the drawing except the group I selected and everything
it's children-shapes connect to (+their connections).

I am having trouble with the code you gave. For example, the name of
the group is "Sheet.675". Would the code be "Sheet.675.Shapes.Count"?
I tried that and an error message came up, saying "Compile Error:
Expected end of statement", and highlighting the period between 675
and Shapes.
 
J

John... Visio MVP

Hi Paul.

I would like to select a group, and then run a macro that hides
everything in the drawing except the group I selected and everything
it's children-shapes connect to (+their connections).

I am having trouble with the code you gave. For example, the name of
the group is "Sheet.675". Would the code be "Sheet.675.Shapes.Count"?
I tried that and an error message came up, saying "Compile Error:
Expected end of statement", and highlighting the period between 675
and Shapes.


You would use something like
Debug.Print ActivePage.Shapes("Sheet.675").Shapes.Count

If you are going to work with that shape, a better approach would be
dim shp as visio.shape
set shp = ActivePage.Shapes("Sheet.675")
debug.print shp.Shapes.Count

Since you are using a selection, this form may be preferable
set shp = ActiveWindow.Selection(1)
This will select the first shape in the selection list

I would also suggest looking at layers rather than playing with the actual
shapes. Use the layer to control what shapes are visible.

John... Visio MVP
 
V

vespasiandamascus

Hi John, thanks for the help. I can use the piece of code you gave me
to find the count, so I know it works. One thing that is still
confusing me is how to loop through the group members. I've got the
following:

Dim cmd As Visio.Shape
Dim mem As Visio.Shape
Dim con As Visio.Connects

Debug.Print cmd.Shapes.Count

Set cmd = ActivePage.Shapes("Sheet.675")
For Each shp in cmd
Set con = shp.Connects
Next shp
 
P

Paul Herber

Hi John, thanks for the help. I can use the piece of code you gave me
to find the count, so I know it works. One thing that is still
confusing me is how to loop through the group members. I've got the
following:

Dim cmd As Visio.Shape
Dim mem As Visio.Shape
Dim con As Visio.Connects

Debug.Print cmd.Shapes.Count

Set cmd = ActivePage.Shapes("Sheet.675")
For Each shp in cmd
Set con = shp.Connects
Next shp

---
I am getting compilation errors. I thought setting the group variable
to a Visio.Shapes type would make more sense seeing as how there are
multiple shapes, but when I did that it wouldn't even let me do the
debug.print. Do you know any possible sources for the error?

What's the compilation error?
Dim shp as Visio.Shape
 
J

John... Visio MVP

Hi John, thanks for the help. I can use the piece of code you gave me
to find the count, so I know it works. One thing that is still
confusing me is how to loop through the group members. I've got the
following:

Dim cmd As Visio.Shape
Dim mem As Visio.Shape
Dim con As Visio.Connects

Debug.Print cmd.Shapes.Count

Set cmd = ActivePage.Shapes("Sheet.675")
For Each shp in cmd
Set con = shp.Connects
Next shp

---
I am getting compilation errors. I thought setting the group variable
to a Visio.Shapes type would make more sense seeing as how there are
multiple shapes, but when I did that it wouldn't even let me do the
debug.print. Do you know any possible sources for the error?


cmd is a shapes collection so should be dim'd as Visio.Shapes

Since Shapes can contain shapes you will have to add a touch of recursion.


Public Sub ConnectionsList()
Dim vsoSelect As Visio.Selection
Dim vsoShape As Visio.Shape
Dim vsoShapes As Visio.Shapes

Set vsoSelect = Visio.ActiveWindow.Selection

Debug.Print vsoSelect.Count
If vsoSelect.Count > 0 Then
'For each shape in the selection, get its connections.
For Each vsoShape In vsoSelect

Call ProcessShape(shp)

Next vsoShape
Else
MsgBox "You Must Have Something Selected"
End If

End Sub

Public Sub ProcessShape(shp As Visio.Shape)
Dim subshp As Visio.Shape
Dim vsoConnect As Visio.Connect
Dim vsoConnects As Visio.Connects

Debug.Print shp.Name

Set vsoConnects = vsoShape.Connects

'For each connection, get the shape it connects to.
For Each vsoConnect In vsoConnects

'Print the name of the shape the
'Connect object connects to.
Debug.Print vsoShape.Name; " connects to "; vsoConnect.ToSheet.Name

Next vsoConnect

If shp.Shapes.Count > 0 Then
For Each subshp In shp.Shapes
Call ProcessShape(subshp )
Next subshp
End If

End Sub

John... Visio MVP
 
V

vespasiandamascus

Dim cmd As Visio.Shape(s)
Dim mem As Visio.Shape

Set cmd = ActivePage.Shapes("Sheet.675")
Debug.Print cmd.Shapes.Count

---

This only works when cmd is a Visio.Shape, does not compile when it is
Visio.Shapes. However, I can't loop through them since it is a shape
not a shapes. Should it be a Visio.Selection? It is not a selection,
it is a group. Does that make any difference then? Because I tried the
code you wrote and it didnt compile ("ByRef argument type mismatch").

I'm just trying to get back to basics, I'll forget about the
connections for now since I'm pretty sure I know how to do that. I
just need to figure out a way to loop through the member shapes in a
group, so I can add them to a layer to make them visible I guess. I
know the logic, it's just a matter of choosing the right variable type
I just can't figure it out.

Thanks for your help so far!
 
J

John... Visio MVP

Dim cmd As Visio.Shape(s)
Dim mem As Visio.Shape

Set cmd = ActivePage.Shapes("Sheet.675")
Debug.Print cmd.Shapes.Count

---

This only works when cmd is a Visio.Shape, does not compile when it is
Visio.Shapes. However, I can't loop through them since it is a shape
not a shapes. Should it be a Visio.Selection? It is not a selection,
it is a group. Does that make any difference then? Because I tried the
code you wrote and it didnt compile ("ByRef argument type mismatch").

I'm just trying to get back to basics, I'll forget about the
connections for now since I'm pretty sure I know how to do that. I
just need to figure out a way to loop through the member shapes in a
group, so I can add them to a layer to make them visible I guess. I
know the logic, it's just a matter of choosing the right variable type
I just can't figure it out.

Thanks for your help so far!


That was my mistake. Do not change the Dim of cmd. You need to use

For Each shp in cmd.Shapes

instead of

For Each shp in cmd

That was why I thought cmd should be Visio.shapes, but in the "Set cmd =" is
properly used as a Visio.Shape.

The other answer is a full example

John... Visio MVP
 

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