none
How to delete all grouped shapes? RRS feed

  • Question

  • Hi, 

    I'm using VS 2015 and Excel 2016 to build an addin.

    I've already build a function to add shapes and then grouped them.

    I'd like to know is there a way to delete all grouped shapes?

    Below is what I have now and which don't work.

    Private Sub ClearHyperLink_Click(sender As Object, e As RibbonControlEventArgs) Handles ClearHyperLink.Click
           
    Dim app As Object = Globals.ThisAddIn.Application
    Dim sht As Excel.Worksheet = Nothing
    Dim shp As Excel.Shape = Nothing
            For Each shp In app.ActiveSheet.shapes
                If shp.Name = "box1" Or shp.Name = "line1" Then
                    sht.Shapes.SelectAll()
                    app.selection.delete()
                End If
            Next
    
     End Sub

    In my adding shapes function, I name every shape a name called "box1" or "line1".

    Then, in this delete function I select all shapes which are named "box1" or "line1" and delete the selection.

    I don't know why this couldn't work. Or is there a way to easily select all grouped shapes?

    Any suggestion would be appreciated. Thanks a lot in advance.



    • Edited by AuditorQQ Tuesday, March 14, 2017 11:51 AM
    Tuesday, March 14, 2017 11:48 AM

Answers

  • Hi,

    According to your description,i think you want to delete the group if any shape in it named line1 or box1,right?

    These shapes have a common name like “Group1” after grouping ,so your code does not work because there is no shape named line1 or box1.

    You need iterate through shapes in group so that you can get their name.

    Here is the example

    Private Sub ClearHyperLink_Click(sender As Object, e As RibbonControlEventArgs) Handles ClearHyperLink.Click    
            Dim app As Object = Globals.ThisAddIn.Application
            Dim sht As Excel.Worksheet = Nothing
            Dim shp As Excel.Shape = Nothing
            Dim i As Integer
            sht = app.ActiveWorkbook.ActiveSheet
            For Each shp In sht.Shapes
                If shp.Type = Microsoft.Office.Core.MsoShapeType.msoGroup Then
                    For i = 1 To shp.GroupItems.Count
                        If shp.GroupItems.Item(i).Name = "line1" Then
                            shp.Delete()
                            Exit For
                        End If
                    Next
                End If
            Next
        End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AuditorQQ Wednesday, March 15, 2017 9:35 AM
    Wednesday, March 15, 2017 5:51 AM
    Moderator

All replies

  • Hi,

    According to your description,i think you want to delete the group if any shape in it named line1 or box1,right?

    These shapes have a common name like “Group1” after grouping ,so your code does not work because there is no shape named line1 or box1.

    You need iterate through shapes in group so that you can get their name.

    Here is the example

    Private Sub ClearHyperLink_Click(sender As Object, e As RibbonControlEventArgs) Handles ClearHyperLink.Click    
            Dim app As Object = Globals.ThisAddIn.Application
            Dim sht As Excel.Worksheet = Nothing
            Dim shp As Excel.Shape = Nothing
            Dim i As Integer
            sht = app.ActiveWorkbook.ActiveSheet
            For Each shp In sht.Shapes
                If shp.Type = Microsoft.Office.Core.MsoShapeType.msoGroup Then
                    For i = 1 To shp.GroupItems.Count
                        If shp.GroupItems.Item(i).Name = "line1" Then
                            shp.Delete()
                            Exit For
                        End If
                    Next
                End If
            Next
        End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AuditorQQ Wednesday, March 15, 2017 9:35 AM
    Wednesday, March 15, 2017 5:51 AM
    Moderator
  • Thank you so much, it works.
    Wednesday, March 15, 2017 9:35 AM