[Solved] Place image in Calc via macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
FDB
Posts: 11
Joined: Mon Oct 16, 2023 3:36 pm

[Solved] Place image in Calc via macro

Post by FDB »

I have a simple database contains some across in order to create calc documents with data from the database.
One field in the calc document should contain a picture (Menu : insert picture from file).
I can achieve this via a recorded macro which resides in the calc document itself.

However when trying this from a macro belonging to the database, nothing happens.

Code: Select all

Sub placeFoto

rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(1).Name = "ToPoint"
args1(1).Value = "$C$7"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(3) as new com.sun.star.beans.PropertyValue
args2(1).Name = "FileName"
args2(1).Value = "file:///Volumes/DUVEL$DATA/Data/VRTDB/fotos/BART_HE.jpg"
args2(2).Name = "FilterName"
args2(2).Value = "JPEG - Joint Photographic Experts Group"
args2(3).Name = "AsLink"
args2(3).Value = true
dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args2())

End Sub
Using the UNO dispatcher does not seem to recognise the active calc document,

Code: Select all

Cell = Sheet.getCellRangeByName("fNaam")
Cell.String = Namen.Naam
works without problems

The examples from Andrew Pitonyak (5.9. Loading/Inserting an image into your document) do not work because
oDoc.createInstance("com.sun.star.text.GraphicObject")
is not applicable to calc documents.

help !
Last edited by MrProgrammer on Wed Apr 24, 2024 7:30 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
OpenOffice 4.1.14
OSX 10.14.6 (Mojave)
Mountaineer
Posts: 318
Joined: Sun Sep 06, 2020 8:27 am

Re: Place image in Calc via macro

Post by Mountaineer »

FDB wrote: Tue Mar 19, 2024 5:30 pm ...
However when trying this from a macro belonging to the database, nothing happens....
Check your code: You obtain "document", wich will point to your database, when started there. When you use this reference you can not expect it to be replaced by "address of some Calc sheet, please read my mind"

So you need to open your Calc-Document with some myCalcDocument=OpenFromURL(... then use myCalcDocument instead of document in your Macro.
I dont know if you need to open the file, or find it in the list of open documents, but your macro needs the right refeference to work.
OpenOffice 3.1 on Windows Vista
FDB
Posts: 11
Joined: Mon Oct 16, 2023 3:36 pm

Re: Place image in Calc via macro

Post by FDB »

The calc document (spreadsheet) is created via a template, then values from the database are filled in :

Code: Select all

	TemplateURL =  "FullFicheTemplate.ots"
	fiche = StarDesktop.loadComponentFromURL (TemplateURL,"MyFrame", 0, Dummy())
	Sheet = fiche.Sheets(0)		' only ONE sheet
	...
	Cell = Sheet.getCellRangeByName("fNaam")
	Cell.String = Namen.Naam
Later on, I want to place the picture :

Code: Select all

document   = ThisComponent.CurrentController.Frame	' ++++ THIS NOT THE CALC DOCUMENT
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(1).Name = "ToPoint"
args1(1).Value = "$C$7"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
rem ----------------------------------------------------------------------
dim args2(3) as new com.sun.star.beans.PropertyValue
args2(1).Name = "FileName"
args2(1).Value = "file:///Volumes/DUVEL$DATA/Data/VRTDB/fotos/BART_HE.jpg"
args2(2).Name = "FilterName"
args2(2).Value = "JPEG - Joint Photographic Experts Group"
args2(3).Name = "AsLink"
args2(3).Value = true
dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args2())
Q : how can I pick up the spreadsheet in variable 'document" ?
OpenOffice 4.1.14
OSX 10.14.6 (Mojave)
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Place image in Calc via macro

Post by ms777 »

Code: Select all

document = fiche
FDB
Posts: 11
Joined: Mon Oct 16, 2023 3:36 pm

Re: Place image in Calc via macro

Post by FDB »

Finally I found the culprit :
When in debugger mode, 'document' points to the debugger window.
While the calc window is still active :

Code: Select all

dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
document = StarDesktop.getActiveFrame()	' IF BREAKPOINT BEFORE HERE, THEN DOCUMENT BECOMES DEBUGGER WINDOW
OpenOffice 4.1.14
OSX 10.14.6 (Mojave)
Post Reply