Search code examples
vbapowerpoint

PowerPoint VBA add shape event/ add shape with tag value


i'am currently trying to add a small function to PowerPoint using VBA, my goal is to create a gadget that works kind of like Photoshop graphics layer.

My plan is to add a layer name Tag on each shape that is drawn by user, so later I can parse through every item by loop and preform lock/unlock, show/hide shapes based on it's tag value, such as:

Sub add_shape_with_layer_tag()
    Set islide = ActivePresentation.Slides(1)
    Set ishape = islide.Shapes.AddShape(msoShapeRectangle, 5, 5, 80, 60)
    ishape.Tags.Add "Layer", "1"
End Sub

Sub show_hide_layer_one_shapes()
        Dim active_slide As Slide
        Set active_slide = ActiveWindow.View.Slide
        For Each ishape In active_slide.Shapes
            If ishape.Tags("Layer") = "1" Then
                ishape.Visible = Not (ishape.Visible)
            End If
        Next ishape
End Sub

However, I couldn't found a way to achieve this, so I would like to ask whether there's a method that provides following functions?

  1. override add shape function so I can sneak tag value in to shape every time user drawn a shape

  2. catch add shape event (if this thing did exist) so i can add tag to the last added item

  3. a way to set a default tag value to shape, just like setting default shape color/line width

or is there any better options that is also viable?

thanks.


20200728

To John, thanks for the advice, I did found some interesting event that might be helpful for some of my other projects, however I couldn't found events that able to trigger after custom function while shape is added.

To Steve, my plan is to add a modeless userform with list UI that manage layers, the shape tag and shape fill texture/color will be determined based on what list item that currently selected.

As to saving settings, I'll use VBComponents.CodeModule to dump existing settings in userforms to a VBA module and store as text, so in theory I should able to make this function self contained in one file.


Solution

  • Not totally an answer, sorry SO, but comments don't allow enough scope for this. So ...

    To Steve, my plan is to add a modeless userform with list UI that manage layers, the shape tag and shape fill texture/color will be determined based on what list item that currently selected.

    Ah, so you're creating your own "pseudo-layers". That wasn't clear. Thanks for the add'l info. As it happens, I have a selection manager add-in that works very similarly to what you're proposing.

    To John, thanks for the advice, I did found some interesting event that might be helpful for some of my other projects, however I couldn't found events that able to trigger after custom function while shape is added.

    The SelectionChange event should get you there. When it fires, you'll need to check first to see if the current selection is a shape or something else. If a shape, check to see if its .Index = current slide's .Shapes.Count and also to see if you've already tagged it. If no tag AND if it's the correct index, it'll be a newly added shape. If you're tagging ALL shapes, you may only need to check to see if the .Tag(name) is blank.

    As to saving settings, I'll use VBComponents.CodeModule to dump existing settings in userforms to a VBA module and store as text, so in theory I should able to make this function self contained in one file.

    Why not save any needed slide or presentation level info as further slide- or presentation-level tags? PPT can absorb quite a lot of info as tags w/o getting cranky.