I have a Libreoffice Calc workbook with over 60 sheets many of which have heavy images that were copied to them. I would like to delete all of the images with a Calc Basic macro.
I have tried the following which fails with a Basic runtime error saying Property or method not found: Pictures.
Sub DeleteAllPics()
Dim Pic As Object
For Each Pic In ThisComponent.CurrentController.ActiveSheet.Pictures
Pic.Delete
Next Pic
End Sub
I have also tried the following which fails with Basic runtime error Object variable not set.
Sub deleteAllPics()
Dim wkSheet As Object
For Each wkSheet In ThisWorkbook.ThisComponent.Sheets.getByName()
Dim Pict As Object
For Each Pict In wkSheet
Pict.Delete
Next Pict
Next wkSheet
End Sub
The following code will remove all pictures from all pages of a Libreoffice Writer document:
Sub RemoveImages
Dim oDoc as Object
oDoc = ThisComponent
Dim oGraphics as Object
oGraphics = oDoc.getGraphicObjects()
Dim oImg as Object
For Each oImg in oGraphics
oDoc.getText().removeTextContent(oImg)
Next
End Sub
I need a code that will work like the above one to remove all images from all sheets of a Calc workbook. Please help me.
For spreadsheets, you need to obtain the XDrawPage for each sheet.
Here is proper LibreOffice Basic code. In LibreOffice parlance, it's called a spreadsheet or a document, not a workbook. Also For Each
is borrowed from VBA and does not work for XDrawPage. (Sheets could be enumerated with For Each
, but to be consistent, the standard For
loop is preferred.)
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 = 0 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