1
votes

I have another challenge. Has anyone tried to intercept the copy/paste events? My goal is to prevent someone from cut/copy/paste a shape that is already on a sheet. If they drag it from a stencil, that is fine. I just can't have them duplicate an existing shape. To make this a bit harder, it is only the shape that I need to prevent. If the want to copy text, that is fine.

We have a order type database that contains items that need to be dropped on a Visio sheet. I cannot use the shapeID because I need to be able to update the shape from the database and I can't write the ID back to the database as it is against security policy. The way that I am tying the two together is a property named shapeKey and that value is provided by the database at the time the shape is dropped.

When a use needs to refresh the sheet from the database, I interate through the shapes, comparing the shapeKey in the shape and in the database. if there is a match, I do an update of the other properties. If there is no match, I want to delete the shape. if a user copies a shape, the shapeKey will also be duplicated and that causes problems. Lastly, they can add their own shapes from stencils and those shapes must be excluded from the delete process

I have two options:

  1. Disable shape cut/copy/paste
  2. Intercept the copy/cut and when they paste, change a property so that I know that it was a user pasted shape. This is preferred because it is more user friendly
  3. I just thought if another way I that might work. Is there a way to lock the shapes to prevent the copy? If there is a lock, would that also lock the location? The user needs to be able to most the shapes around.

I thought about capturing the event but I could not find the event codes to look for. I cannot install the Visio SDK which has the Event monitor. The monitor might have shown me the code. Here is some pseudo code as to what I think would be the flow.

option 1

if select item is a shape then
    msgbox "shape copying verboten. please us the stencil"
    clear selected item

option 2

capture the paste event
    if selected item is a shape then
        vsoShape.Cells("Prop.ShapeKey").Formula = Chr(34) & "protect" & Chr(34)

Layers might work. All the database controlled shapes can be on one layers and all the user shapes on another but I haven't worked with layers before. would that work? How can I be sure that any shape pasted goes onto the user layer?

1
Do you want to disable only certain cut/copy/paste, or is disabling all of them an option? Take a look at this example: vbaexpress.com/kb/getarticle.php?kb_id=373 - kponz
that was a great idea and would have work except that the application.OnKey doesn't work for Visio. The code is for Excel. I wish it would have worked, though. It would have been the solution. - M.Ford

1 Answers

1
votes

Thanks all! I did find a solution and it was really elegant. I found the idea here and changed it to what I needed:

Shape added event: https://msdn.microsoft.com/en-us/library/office/ff767288.aspx

Here is what I came up with:

Private Sub Document_ShapeAdded(ByVal vsoShape As Visio.IVShape)
    If vsoShape.CellExistsU("Prop.ShapeName", 0) Then
        vsoShape.CellsU("Prop.ShapeName").Formula = Chr(34) & "ShapeName" & Chr(34)
    End If
End Sub

The interesting thing is that it doesn't fire if I add the shape via VBA. That is perfect for what I need but I would have thought that dropping a shape is adding a shape. I am only looking for one property because not all shapes on the stencils come from a database and for those objects, I don't need to do anything.

It was not the solution that I expected but it works really well.