2017-09-05 114 views
0

我有一個Libreoffice Calc工作簿,其中有60多張圖片,其中很多圖片都被複制到它們中。我想用Calc Basic宏刪除所有的圖像。 我已經試過這失敗,一個基本的運行時錯誤說Property or method not found: Pictures.如何從LibreOffice Calc工作簿的所有工作表中刪除所有圖像

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

對於電子表格,您需要爲每張表格獲取XDrawPage

這裏是正確的LibreOffice基本代碼。按照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 
+0

是的,完美的作品。所有的圖像都從所有的Calc文檔中刪除。非常感謝你的回答和解釋。 –

相關問題