DotNetSpartan
DotNetSpartan

Reputation: 1001

How to get the details of a group object in MS Visio through C#

I have a shape object drawn as a 'group' with text as 'P0', The group object contains two other small shape objects with text as 'P1'.

enter image description here

How can i get the details of this group object such that i can have an access to the text of all three shape objects - 'P0', 'P1', 'P1'. Any help will be much appreciated.

Upvotes: 1

Views: 331

Answers (1)

JohnGoldsmith
JohnGoldsmith

Reputation: 2698

Assuming that:

  • the target shape is a group shape (and not a container)
  • the two sub-shapes are direct children of the group

then the following would work:

//Some method to get your target shape
var shp = vApp.ActivePage.Shapes.ItemFromID[1];

shp.Characters.Text.Dump($"Group shape - ({shp.NameID})");
foreach (Visio.Shape s in shp.Shapes)
{
    s.Characters.Text.Dump($"Sub shape - ({s.NameID})");
}

Note - the Dump method is just an extension method in LINQPad, but you could replace this with Console.WriteLine or similar.

The above code would produce output similar to this (where I've changed the text of the second sub-shape to 'P2'):

enter image description here

Also, you could just use the Shape.Text property directly, but any fields in the text won't be expanded. So to get the full text as the user sees it, you use Shape.Characters.Text instead.

Upvotes: 2

Related Questions