5

I'm working on a Add-in for PowerPoint 2010 (C#) and I want to prevent the end-user to move or edit all the shapes that I have programmatically created.

I have already sought in the framework but I think it's not allowed programmaticaly. Has anyone already encountered this kind of limitations and could help me to find a solution?

I know that some people create their add-in thanks to C++ because there are a lot of limitations in office.

monstergold
  • 755
  • 1
  • 9
  • 24
  • Question edited : I have tried to be as precise as possible – monstergold Aug 05 '13 at 18:39
  • 1
    There's nothing in the object model that allows you to lock PowerPoint shapes. There's a way to do it via the XML that underlies PPT 2007 and onward. Check this thread for a link to John Wilson, who's worked out how to do it: http://answers.microsoft.com/en-us/office/forum/office_2007-powerpoint/locking-shapes/945e3424-4994-488c-a575-8e7a94c76925 The other approach is to trap the selection change event and if the newly selected shape is a locked shape, set it back to a known position once the selection changes again. – Steve Rindsberg Aug 05 '13 at 18:49
  • I found this sample code to detect all the events occuring thanks to commandsBars.OnUpdate : http://code.msdn.microsoft.com/CSExcelNewEventForShapes-0e26b1f2#content It works but it's not an ideal solution – monstergold Aug 06 '13 at 10:12

2 Answers2

4

I have found two solutions :

  • The first is to catch all events from the "commandBars.OnUpdate" like this great sample code : http://code.msdn.microsoft.com/CSExcelNewEventForShapes-0e26b1f2#content Then you can impose the position/the color or everything you want to your shape.
  • The second one is more "brutal" > unselect immediately the shape. When you catch all the events from the "CommandBars.OnUpdate" do this :

To see which shape is selected :

var selectedShape = this.Application.ActiveWindow.Selection.ShapeRange[1]

In all my shapes, I have set a tag with an ID. I have just to check that there are an ID in the tags of the selectedShape and if this is the case :

this.Application.ActiveWindow.Selection.Unselect();

Then I show a messageBox to warn the user to do not select this kind of shape. I don't like this solution but it's the only one that I have found and it works.

monstergold
  • 755
  • 1
  • 9
  • 24
  • You intercept the PowerPoint's built-in behaviours in an unnatural way. What if the user clicks on "undo" button after you unselect the shape? The shape get selected again. My advice is never try to lock the shape, because we cannot control the undo list. – chipbk10 Oct 10 '14 at 15:17
  • 1
    you are right. I've tried it. Unselecting a shape is not stored in the undo stack. – chipbk10 Oct 13 '14 at 09:00
2

I believe this is not possible. A way of achieving this to a certain extent (people can work around it if they figure out how to select the shapes below) is by making a transparent rectangle the size of the canvas and binding a custom event to that (like you described in your comment). The transparent rectangle is overlaying the shapes you created so people can no longer access the shapes that way. Of course if they are capable of figuring out how to select the shapes they can move them anyway...

Alternatively, to make people not do stuff like that (you only stop the inexperienced) you can also set them up as master slides.

Only 'real' solution for people not doing that? Images .. but then they can move the image too!

ranieuwe
  • 2,268
  • 1
  • 24
  • 30
  • Since OP mentioned wanting to "lock" all the shapes that s/he has programmatically created, but not necessarily ALL shapes, the transparent rectangle and image tricks wouldn't necessarily work anyhow. But as you suggest, a determined idiot can nearly always overpower the smartest software. The goal should generally be to make it easy for users to do what they need to do rather than attempting to prevent them from doing what they shouldn't. – Steve Rindsberg Aug 14 '13 at 14:17