Eddie
Eddie

Reputation: 621

Excel VBA: Right click on a shape and add a new menu item

I am using Excel 2007 on a Windows XP Machine. On my worksheet I have lots of 'Shapes' (Rectangles).

I have assigned macro's to the Shapes so that they perform a task when someone clicks on them.

What I would like to do is the following, but I've read posts which say that it's not possible.

It's easy to do this when someone right clicks on a cell, but shapes seem different.

I know that the worksheet has an event called 'Worksheet_BeforeRightClick' but I can't get this to work when I right click on a shape.

Below is the start of my code but it doesn't have any effect on my menu's.

Many Thanks in advance

With Application.CommandBars("Shapes")
    With .Controls.Add
        .Caption = "My New Menu Item"
        .BeginGroup = True
        .OnAction = "MyNewMacro"
    End With
End With

Upvotes: 1

Views: 2826

Answers (1)

Takedasama
Takedasama

Reputation: 387

As I understand, You want to add a macro that, when ran, would create another shape (?). IF that's true, you can run something like:

Sub AddShape ()
Sheets("YourSheetName").Shapes.AddShape(msoShapeRectangle, 35.25, 30.75, 121.5, 51.75).Select
'Add some additional info like name and style, or even attach another macro to it:    
Selection.ShapeRange.Name = "Test_Name"
Selection.ShapeRange.ShapeStyle = msoShapeStylePreset36
Selection.OnAction = "Testmacro"
End Sub

Sub TestMacro
MsgBox "Hello"    
EndSub

Upvotes: 0

Related Questions