[Writer] How to remove all pictures from a document?

Hey, as title said, how would I go about removing all the pictures from a document in LibreOffice Writer?

Thank you.

This answer is drawn from “How to remove all the images in a document in LibreOffice Writer?” on AskUbuntu StackExchange. Thanks @chaskes!


The easiest way to do this is via a macro. The following steps will make it availabe as a Toolbar menu item.

  1. Click on Tools > Macros > Organise macros > LibreOffice Basic... (a new window opens).

  2. In the left-hand box, you should see “[+] My Macros”. Open that up by clicking on the “plus” sign to get “[+] Standard”, open it and see “Module 1”, now click on it. You should now see “Main” listed in the right-hand box, “Existing macros in: Module 1”.

  3. Click on “Main” in the right-hand box, and now the “Edit” button to the right of it. A new window opens, and in the main text area, you should see “Sub Main” followed a blank line later by “End Sub”. Good.

  4. Now we need to get the LO Basic code to paste in below that “End Sub” line. Copy this code block:

     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
    
  5. Return to the LibreOffice Macro editor, position your cursor under the “End Sub” line, then hit CTRL-V to paste it in. Press CTRL-S to save it.

  6. Close the Macro editor.

You could just run the macro from Tools > Macros > Organise macros > LibreOffice Basic..., click on RemoveImages, then click “Run” (the top button on the right). If you’re doing this infrequently, that’s probably good enough. If you think you might be using this more often, then it can be assigned to the menus by following these steps:

  1. Go Customise > Menus, then choose the menu you want to add your macro to from the Menu drop-down. I used “Edit”.
  2. Click the Add... button on the right of the dialog, and scroll to the bottom of the “Category” listing until you see > LibreOffice Macros.
  3. Click (to open) on My Macros > Standard > Module 1. You should now see “RemoveImages” listed in the right-hand “Commands” box. Click on it to select it.
  4. Click the Add button (which will add the macro to your chosen menu), then Close, to close the “Add Commands” dialog.
  5. You can now use the and buttons to locate the “RemoveImages” macro precisely where you want it in the menu. When finished, click OK.

Removing all the images in your Writer document is now only one click away.

thanks this works perfectly.
i would upvote but i dont have enough rep

I tried this (Dec 2017) and got error “BASIC runtime error.
Property or method not found: getGraphicObjects” on Windows 7 LO Version: 5.4.2.2 (x64)
Any suggestions on how to fix this?

delete On all pages (works in 2020)

  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

getByIndex(i) is likely wrong - needs to be getByIndex(iShape)

And use Long, not Integer.