Visio Align Shapes Programmatically

L

LadySnowWhite

How can I align specific shapes programmatically using C#? I think there
should be a method for it, but I can't find it. Currently, I'm just randomly
dropping shapes and calling the Layout method, which cleans it up a bit, but
I would like all shapes A to align left ,and all shapes B to align right, and
all shapes C centered. Can someone please help me?
 
W

WapperDude

If you run the macro recorder to establish the basic steps -- select shapes,
align them -- you'll get the basic syntax. You'll have to convert from VBA
to C#, though.

HTH
Wapperdude
 
L

LadySnowWhite

Thank you for your suggestion, WapperDude. I had thought about doing that
and after you suggested that, I went ahead and tried it. Here’s what I got:

Sub Macro1

ActiveWindow.DeselectAll
‘shape A
ActiveWindow.Select Application.ActiveWindo.Page.Shapes.ItemFromID(1),
visSelect
ActiveWindow.Select Application.ActiveWindo.Page.Shapes.ItemFromID(2),
visSelect
Application.ActiveWindow.Selection.Align visHorzAlignLeft, visVertAlignNone,
False

ActiveWindow.DeselectAll
‘shape B
ActiveWindow.Select Application.ActiveWindo.Page.Shapes.ItemFromID(3),
visSelect
ActiveWindow.Select Application.ActiveWindo.Page.Shapes.ItemFromID(4),
visSelect
Application.ActiveWindow.Selection.Align visHorzAlignRight,
visVertAlignNone, False

End Sub


I looked up the method visHorzAlign and this result, which confirms what we
did, but doesn’t help me further,
http://msdn.microsoft.com/en-us/library/ms367516.aspx.

I have no idea how to translate this into C#, but I could leave it as just a
macro if I can find a way to loop it so that no matter how many Shapes A and
B I have, it will align them.

Something like this maybe:

For (int i = 0; i<= NumA; i++)
{
ActiveWindow.Select Application.ActiveWindo.Page.Shapes.ItemFromID(i),
visSelect
Application.ActiveWindow.Selection.Align visHorzAlignLeft, visVertAlignNone,
False
}

For (int i = NumA; i<= NumB; i++)
{
ActiveWindow.Select Application.ActiveWindo.Page.Shapes.ItemFromID(i),
visSelect
Application.ActiveWindow.Selection.Align visHorzAlignLeft, visVertAlignNone,
False
}


I’ll have to check my VBA syntax of course, but since the program drops
items sequentially (ItemFromID) and drops all the A items first and B items
next, it should be fine for i to increment accordingly. What I don’t know
how to do is to find NumA and NumB.

A and B shapes come from a database and the data is linked to each shape.
Is there a VBA function to get the count or size of my linked database or
otherwise run queries on it from Visio?

Also, the align functions line the shapes up with respect to each other. Is
there a way to justify them all the way to one side? So that if they are all
dropped on the right side, I can push all of them to the far left side?

Many thanks for your help.
 
W

WapperDude

Programming isn't my strong point, but, here's a couple of thoughts:

1.) You can setup a loop to do all of the shapes
2.) Within the loop, test for the shape type, say, by shape name.
3.) With all of one shape type selected, do the align
4.) Group the shapes
5.) Move the group to desired spot on the page.
6.) Ungroup and de-select.

Repeat the process for each shape type.

You might take a look at John Marshall's website,
http://visio.mvps.org/VBA.htm, which has a lot of VBA examples. VisioGuy's
site, http://visguy.com/vgforum/index.php?board=5.20, also covers a variety
of programming issues.

Wapperdude
 
P

Paul Herber

Thank you for your suggestion, WapperDude. I had thought about doing that
and after you suggested that, I went ahead and tried it. Here’s what I got:

as your generated macro code suggests
1. deselect all shapes
2. select the shapes you want to align using the
ActiveWindow.Select method
3. align the selected shapes using the
ActiveWindow.Selection.Align method
 
L

LadySnowWhite

Thank you so much, WapperDude! You're suggestions look promising and I shall
investigate them first thing tomorrow morning. I really appreciate your
detailed responses and all of your suggestions. I will keep you posted on my
progress.

Sincerely,
LadySnowWhite
 

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