Latheesan
Latheesan

Reputation: 24116

PowerPoint VSTO Add-in - add a shape with a tag

I would like to programatically add a shape into the current slide of PowerPoint in C# .NET

So, I have created a VSTO Add-in with a ribbon which has a button "Add Shape". When this button is clicked, the following code executes:

Globals.ThisAddIn.Application.ActiveWindow.View.Slide.Shapes.AddShape(
    Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 0, 0, 10, 20);

When this code executes, it correctly adds a rectangle shape into the current slide.

I would like to modify this shape later (for example; change it's width). To be able to do this, I read somewhere that the shape should have a unique identifier and this can be achieved using a Tag?

If so, how do you add a shape and set a tag on the newly added shape, so that I can manipulate it later?

Also, how do you iterate through the collection of shapes on the current slide, so that you can check the tag of the shape to see if that's the one I want to modify?

Upvotes: 0

Views: 1740

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

The .AddShape method can return a reference to the shape you've just added. You'll want to do that for simplicity's sake.

The shape has a .Tags object The .Tags object has an .Add method

So assuming a reference to the shape in oShape, you can do (again in VBA, you'll have to translate):

oShape.Tags.Add "MyTagName", "MyTagValue"

For more information and an example (VBA) function that returns a reference to a shape with a given tag value, you can visit this page on my PPT FAQ site:

Working with Tags (and a bit about Functions) http://www.pptfaq.com/FAQ00815_Working_with_Tags_-and_a_bit_about_Functions-.htm

Upvotes: 1

Related Questions