如何从Libreoffice Calc工作簿的所有工作表中删除所有图像

时间:2017-09-05 06:53:27

标签: libreoffice libreoffice-calc

我有一张超过60张的Libreoffice Calc工作簿,其中许多都有大量复制的图像。我想用Calc Basic宏删除所有图像。 我尝试了以下操作失败,并出现基本运行时错误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

我还尝试了以下因基本运行时错误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

以下代码将删除Libreoffice Writer文档所有页面中的所有图片:

   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

我需要一个像上面一样的代码来删除Calc工作簿的所有图片中的所有图像。请帮帮我。

1 个答案:

答案 0 :(得分:4)

对于电子表格,您需要为每张表格获取XDrawPage

这是适当的LibreOffice Basic代码。在LibreOffice的说法中,它被称为电子表格或文档,而不是工作簿。另外For Each是从VBA借来的,不适用于XDrawPage。 (可以使用For Each枚举表格,但为了保持一致,首选标准For循环。)

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