Ask Your Question
0

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

asked 2018-12-29 04:46:01 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

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!!

edit retag flag offensive close merge delete

Comments

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

Ratslinger gravatar imageRatslinger ( 2018-12-29 04:51:25 +0100 )edit

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

mxc5425 gravatar imagemxc5425 ( 2018-12-29 19:38:59 +0100 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2018-12-29 05:56:42 +0100

Ratslinger gravatar image

updated 2018-12-29 07:11:40 +0100

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 ✔ (upper left area of answer). It helps others to know there was an accepted answer.

edit flag offensive delete link more
0

answered 2018-12-29 19:39:41 +0100

mxc5425 gravatar image

Thank you! It's working now :)

edit flag offensive delete link more

Comments

@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.

Ratslinger gravatar imageRatslinger ( 2018-12-29 19:50:08 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-12-29 04:46:01 +0100

Seen: 49 times

Last updated: Dec 29 '18