none
How to get selected shape in excel vba?

    Question

  • I have inserted a smart art, and converted it to shapes. And Selected a shape by clicking on it.

    Now I want to get Shape object of slected shape. I have tried this but it throws exception.

    dim shap as Excel.Shape = ExcelApp.Selection 

    I can get the shape object by iterating on ActiveSheet.Shapes or like this
    dim shap as Excel.Shape = ActiveSheet.Shapes.Item(1) 

    But how would I know this shape is selected or not, Really need Help Thanks ALot.

    Wednesday, November 23, 2011 5:24 PM

Answers

  • Try this,

        Dim shpTemp As Excel.Shape
        
        Set shpTemp = Application.Selection.ShapeRange.Item(1)
    
    



    Cheers,

    Andy
    www.andypope.info

    Thursday, November 24, 2011 8:33 AM
  • I tried the same code this morning while helping out a similar problem in C#,  the Selection method returns an object and not an excel shape.

    dim shap as object = ExcelApp.Selection 

    or

    dim shap as variant = ExcelApp.Selection 


    jdweng
    Wednesday, November 23, 2011 5:37 PM
  • Hello:

    The code should have been:

    Public Sub ActiveSheetShapes()
    Dim shapObject As Variant
    
    If TypeName(Application.Selection) = "GroupObject" Then
        Set shapObject = Application.Selection
    Else
        Exit Sub
    End If
    
    MsgBox ("Shape Name Selected Is " & shapObject.Name)
    
    End Sub
    
    Regards,


    Rich Locus, Logicwurks, LLC

    http://www.logicwurks.com

    Wednesday, November 23, 2011 6:52 PM

All replies

  • I tried the same code this morning while helping out a similar problem in C#,  the Selection method returns an object and not an excel shape.

    dim shap as object = ExcelApp.Selection 

    or

    dim shap as variant = ExcelApp.Selection 


    jdweng
    Wednesday, November 23, 2011 5:37 PM
  • Hello:

    Here's a little snippet which I will explain after the example:

     

    Public Sub ActiveSheetShapes()
    Dim shapObject As Variant
    
    If TypeName(Application.Selection) = "GroupObject" Then
        Set shapObject = Application.Selection
    End If
    
    MsgBox ("Shape Name Selected Is " & shapObject.Name)
    
    End Sub
    

    If you are in a module and you want to determine if the item selected on the worksheet is a shape, the item selected will be a "GroupObject" if you converted smart art to a shape.  If it is an Oval, the TypeName will be "Oval".  So, you will need to determine which type of shape to test for.  Once you know it is a shape, you can assign it to a Shape Object and use the usual properties, such as name, to examine the shape object.  Is this what you were trying to accomplish?  You will need to change the code above to test to make sure you don't try to display or use the shape object if it isn't a shape object :)

    Regards,

     


    Rich Locus, Logicwurks, LLC

    http://www.logicwurks.com



    • Edited by RichLocus Wednesday, November 23, 2011 6:49 PM
    Wednesday, November 23, 2011 6:44 PM
  • Hello:

    The code should have been:

    Public Sub ActiveSheetShapes()
    Dim shapObject As Variant
    
    If TypeName(Application.Selection) = "GroupObject" Then
        Set shapObject = Application.Selection
    Else
        Exit Sub
    End If
    
    MsgBox ("Shape Name Selected Is " & shapObject.Name)
    
    End Sub
    
    Regards,


    Rich Locus, Logicwurks, LLC

    http://www.logicwurks.com

    Wednesday, November 23, 2011 6:52 PM
  • Try this,

        Dim shpTemp As Excel.Shape
        
        Set shpTemp = Application.Selection.ShapeRange.Item(1)
    
    



    Cheers,

    Andy
    www.andypope.info

    Thursday, November 24, 2011 8:33 AM