如何从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)

2 years ago

对于电子表格,您需要为每张表格获取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