Ask Your Question

Macros and deleting [closed]

asked 2012-04-12 01:57:42 +0100

this post is marked as community wiki

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

I'm attempting to delete a series of shapes, on a Calc spreadsheet, with a macro. Is there an easy way to program this in the Basic IDE? I think I've found a few possibilities but I'm having a hard time wrapping my head around the syntax being used.


edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2015-10-21 18:16:52.611186

3 Answers

Sort by » oldest newest most voted

answered 2012-06-11 22:40:21 +0100

JohnSUN gravatar image

updated 2013-02-13 23:53:47 +0100

qubit gravatar image

Maybe the syntax may seem complicated. But it seems to me that the code is read simply:

REM # Remove all shapes from all sheets of current workbook
Sub clearGraphs
Dim oDrawPages As Variant   REM # All pages of current workbook
Dim oDrawPage As Variant    REM # One (next) draw page
Dim oShape As Variant       REM # One graph object
Dim i As Long, j As Long    REM # Indexes for cycles
    oDrawPages = ThisComponent.getDrawPages()
    If oDrawPages.hasElements() Then
        For i = 0 To oDrawPages.getCount() - 1  REM # For all drawpages in workbook
            oDrawPage = oDrawPages.getByIndex(i)
            If oDrawPage.hasElements() Then
REM # This For...Next - by descending: after removing object all indexes will be shifting
                For j = (oDrawPage.getCount() - 1) To 0 Step -1
                    oShape = oDrawPage.getByIndex(j)
REM # Here may be a check any  properties of  object's oShape
                If (oShape. ..) Then
                Next j
        Next i
End Sub
edit flag offensive delete link more

answered 2012-12-07 21:26:50 +0100

this post is marked as community wiki

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

I am only beginning to understand drawpage but try ignoring ThisComponent.getDrawPages() instead, cycle through each sheet. Then grab it's drawpage and cycle through it's shapes. I think that there is only zero or one drawpage per sheet. aside: The guy that taught me programming (many years ago) never trusted functionality that a language's compiler didn't use. I am inclined to apply the same principle to libreoffice. I think that in use, the developers will regularly refer to the getDrawPage on a sheet, but they might never use getDrawPages() in the product at all.

edit flag offensive delete link more

answered 2012-04-12 07:18:22 +0100

luyu gravatar image

The easiest way is to use macro recording. To enable the feature, go to Tools -> Options -> LibreOffice -> General and check "Enable experimental (unstable) features". A new entry "Record Macro" will appear in Tools -> Macros. Once you click on it, macro recording mode will start and almost everything (for more on this topic and the list of actions that are not recorded read Recording a Macro) you do while in it will be saved once you click "Stop Recording" button. You will then be asked to save newly created macro, given the choice of storing it only for the current document or being generally accessible.

If you are interested in seeing the code generated this way, open Tools -> Macros -> Organize Macros -> LibreOffice Basic, select macro and click "Edit".

edit flag offensive delete link more


the macro recorder isn't all that much use in this case. At best it provides a very clunky workaround method to what should be a simple identify as object and delete loop. Only, I don't know how to code that loop with the unfamiliar libreoffice structures

worntreads gravatar imageworntreads ( 2012-04-13 00:58:50 +0100 )edit

Question Tools


Asked: 2012-04-12 01:57:42 +0100

Seen: 4,047 times

Last updated: Feb 13 '13