


LibreOffice Writer macro to save all the images in the document to separate files. Filenames are automatically generated from image name/ID. All images are created in user/Pictures directory by default.
Copy following code snippet into clipboard.
Sub SaveIMGs
' Save all images in the document to .png files
' in user's %HOME%/Pictures directory
Dim MP(1) As New com.sun.star.beans.PropertyValue
Dim oDoc As Object
Dim oGraphics As Object
Dim oProv As Object
Dim oImg As Object
oProv = createUnoService("com.sun.star.graphic.GraphicProvider")
oDoc = ThisComponent
oGraphics = oDoc.getGraphicObjects()
hd = Environ("HOMEPATH")
If InStr(hd, "\") > 0 Then
'Windows
hd = "/C:" + join(split(hd, "\"), "/")
ELSE
'Linux/MacOS
hd = Environ("HOME")
End If
If hd = "" Then
MsgBox "ERROR: Cannot locate user's home directory!"
Else
For Each oImg in oGraphics
MP(0).Name = "URL"
MP(0).Value = "file://" & hd & "/Pictures/" & oImg.Name & ".png"
MP(1).Name = "MimeType"
MP(1).Value = "image/png"
oProv.storeGraphic(oImg.Graphic, MP)
Next
End If
End Sub
Tools -> Macros -> Edit macros (note: macros must be enabled, this is not the default in LibreOffice!)My Macros & Dialogs -> Standard -> Module1
Main code blockSub SaveIMGs" is created.Tools -> Macros -> Run Macro...
SaveIMGs and click Runuser/Pictures directory by default.This macro is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This macro is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Copy of the GNU General Public License is availabel at: http://www.gnu.org/licenses/
Published date: 03 Sep 2020.