Hi! I'm trying to create a macro that will remove images from a specific tab

I’m working on a calc spreadsheet and currently have 3 tabs “Instructions”, “Raw_Data”, and “Clean_Data”. I have this code so far but it deletes all images in the entire workbook.

Sub DeleteAllPics()
    Dim oDoc As Object
    Dim oDrawPage As Object
    Dim oShape As Object
    Dim iShape As Integer
    Dim iSheet As Integer
    oDoc = ThisComponent
    For iSheet = 1 To oDoc.getSheets().getCount() - 1
        oDrawPage = oDoc.getSheets().getByIndex(iSheet).getDrawPage()
        For iShape = oDrawPage.getCount() - 1 To 0 Step -1
            oShape = oDrawPage.getByIndex(i)
            oDrawPage.remove(oShape)
        Next iShape
    Next iSheet
End Sub

Any help/explanations are appreciated!!

@mxc5425 Please do not check post as wiki when asking a question or posting an answer. It helps no one.

Thanks! I’ll make note of this in the future. This is my first time posting.

Hello,

There are quite a number of items wrong with the code presented. For one, you are deleting not just images but shapes and controls and other items from every sheet. You need to determine what exactly what you want to delete. Sometimes it is a named item or maybe a type of object. Your code also loops through all sheets. You just need the name sheet you want. Example code:

Sub DeleteAllPics()
    Dim oDoc As Object
    Dim oDrawPage As Object
    Dim oShape As Object
    Dim iShape As Integer
    Dim iSheet As Integer
    oDrawPage = ThisComponent.getSheets().getByName("YOUR_SHEET_NAME_HERE").getDrawPage()
    iCount = oDrawPage.Count
    for iShape = iCount - 1 to 0 Step -1
        oShape = oDrawPage.getByIndex(iShape)
Rem Check here if item obtained is one you want
Rem Example selections
'        If oShape.Name = "MyImage" then                                          'By Name
'        If oShape.ShapeType = "com.sun.star.drawing.CustomShape" then            'By Type
'        If oShape.ShapeType = "com.sun.star.drawing.GraphicObjectShape" then     'By Type
'           oDrawPage.remove(oShape)                   'then delete if meets criteria
'        End If                                                           '
    Next iShape
End Sub

If this answers your question please tick the :heavy_check_mark: (upper left area of answer). It helps others to know there was an accepted answer.

1 Like

Thank you! It’s working now :slight_smile:

@mxc5425 Also of note, do not respond as an answer. This is to be used to respond to original question only. You should instead use add a comment under the answer you are responding to.