[Solved] Delete embedded pictures in spreadsheet with macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
arunppai
Posts: 8
Joined: Sun Jan 23, 2022 2:25 pm

[Solved] Delete embedded pictures in spreadsheet with macro

Post by arunppai »

Dear Friends, Hello,

I am using Open Office 4.1.14, Windows 8.1 is the OS.
I use Open Office Visual Basic extensively.

Every month, I download financial statements from the net, and save in "Excel Format".
Subsequently I open the file in Open Office 4.1.14 as a calc document, and save it as an ".ods" file (I don't have Microsoft Excel).

Then with the help of Macros written in Open Office Basic the data is extracted and formated for analysis and storage.

These downloaded files come with embedded pictures, logos etc, which I have to pick and delete.

Please could any one give a few lines of VB code, as to how to get / enumerate these objects and delete them in one go.

Before posting this problem, I searched the forum and also read "OOo_Macros_Pitonyak.pdf" but did not find answers.

Thank you in advance.

Have a nice day.
Last edited by Hagar Delest on Sun Aug 06, 2023 10:22 am, edited 2 times in total.
Reason: tagged solved.
OpenOffice 4.1.7
Windows 8.1
User avatar
Villeroy
Volunteer
Posts: 31355
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Need Open Office VB code to delete embedded pictures from Calc document

Post by Villeroy »

If the unwanted pictures are the only unwanted objects:
Hit Ctrl+A to select all cells on the sheet.
Hit the Delete key (or menu:Edit>Delete Contents), choose "Objects", [OK], Done.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 3127
Joined: Wed Mar 09, 2016 2:40 pm

Re: Need OpenOffice VB code to delete embedded pictures from Calc document

Post by JeJe »

Villeroy's solution, if sufficient, can be turned into a macro using the macro recorder.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Need OpenOffice VB code to delete embedded pictures from Calc document

Post by musikai »

Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
User avatar
Zizi64
Volunteer
Posts: 11497
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Need OpenOffice VB code to delete embedded pictures from Calc document

Post by Zizi64 »

Every month, I download financial statements from the net, and save in "Excel Format".
Does it means a "Copy-Paste" procedure, or it is a real "downloading" procedure? If you make a copy-paste, then you can paste the content as unformatted text (only the text and numbers, but not the other objects) into your spreadsheet file.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Mountaineer
Posts: 335
Joined: Sun Sep 06, 2020 8:27 am

Re: Need OpenOffice VB code to delete embedded pictures from Calc document

Post by Mountaineer »

arunppai wrote: Tue Jul 25, 2023 8:02 am ...
These downloaded files come with embedded pictures, logos etc, which I have to pick and delete.

Please could any one give a few lines of VB code,
...
If the above suggestions are not fitting, please provide an example file. I don't think it will be your private account, but data for stocks or crypto-currencies, but remove/replace private data before upload.

It is much easier to discuss problems with fitting data, than without...

PS: You don't need VB code, as Visual Basic is the implementation and API MS uses in MS-Office. Aoo/LO use their own API named UNO, and there are also some differences between Star-Basic and Visual Basic.
LibreOffice 7.6 on Windows 10pro and other Versions parallel
arunppai
Posts: 8
Joined: Sun Jan 23, 2022 2:25 pm

Re: [Dropped] Delete embedded pictures in spreadsheet with macro

Post by arunppai »

Hello Friends,

Thank you for all your suggestions.

This is what I did and it worked.

Option Explicit
Option Compatible
Sub Main
     CompatibilityMode(True)
     ClearDefinedRange
End Sub

Private Sub ClearDefinedRange
     REM Refer page 157 / 527 UsefulMacroInfo_AndrewMacro.pdf
     Dim oDoc As Object, oSheet As Object, oSheets As Object
     Dim oCellRange As Object
     
     
     oDoc  = ThisComponent
     oSheets = oDoc.Sheets
     
     REM get the sheet & the range
     oSheet = oSheets.getByName("Relevant Sheet")
     oCellRange = oSheet.getCellRangeByName("The Range")

     REM To keep data and delete objects etc
     oCellRange.clearContents(_          
          com.sun.star.sheet.CellFlags.HARDATTR OR _
          com.sun.star.sheet.CellFlags.STYLES OR _
          com.sun.star.sheet.CellFlags.OBJECTS OR _
          com.sun.star.sheet.CellFlags.EDITATTR)
End Sub


Trouble being, I did not apply my mind to my master's (Pitonyak's books) book which is my guiding principle.
Becuase, OOo unlike Microsoft VB is very difficult to comprehend.

In fact the above Macro also made the following lines reduntant.

With CellRange
.merge(False)
.IsTextWrapped = False
                REM and many more lines like this
                REM not necessary.
End With

Thank you very much, have a nice weekend.

 Edit: Added formatting tags -- MrProgrammer, forum moderator 
OpenOffice 4.1.7
Windows 8.1
Post Reply