[Solved] How to insert an image into footer of Calc?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

[Solved] How to insert an image into footer of Calc?

Post by vtssi »

Hello,
can you help me please with this task:
insert an image into footer with Visual Basic

I try to record macro and get this code:

Code: Select all

sub Main3
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 ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:PageFormatDialog", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array())


end sub
It only shows Page Format Dialog.
Last edited by vtssi on Tue Jul 26, 2016 1:04 pm, edited 2 times in total.
LibreOffice 5.1.4.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to insert an image into footer with Visual Basic?

Post by Zizi64 »

with Visual Basic
It is not Visual Basic. It is the Application Programming Interface (API), and the API functions is callaed from StarBasic.

You need get the applied Page Style. The Page Style has Footer and Header properties/objects. You can get them by API functions.

Do you want to insert a picture into the footer of a Sheet of a Calc document, or into the footer of a Writer document?

Examples and tutorials:
https://www.google.hu/url?sa=t&rct=j&q= ... bs.2,d.bGg

http://michal.kosmulski.org/computing/a ... acros.html
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.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to insert an image into footer with Visual Basic?

Post by FJCC »

Here is a quick example but as Tibor says, you have to study the API to do understand the code.

Code: Select all

Dim oSize As New com.sun.star.awt.Size
Dim oPos As New com.sun.star.awt.Point
GrphObj = ThisComponent.createInstance("com.sun.star.text.TextGraphicObject")
GrphURL = convertToURL("c:\users\fjcc\desktop\Image.jpg")
GrphObj.GraphicURL = GrphURL
oSize.Height = 1000
oSize.Width = 2000
GrphObj.Size = oSize
GrphObj.HoriOrient = 0 ' 0 = Left Justified, 1 = Right, 2 = Centered

oStyleFamilies = ThisComponent.getStyleFamilies()
oObj1 = oStyleFamilies.getByName("PageStyles")
oObj2 = oObj1.getByName("Standard")
  
oFooterTextLeft = oObj2.FooterTextLeft
oFooterTextLeft.insertTextContent(oFooterTextLeft.Start, GrphObj, False)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

Zizi64 wrote:
with Visual Basic
It is not Visual Basic. It is the Application Programming Interface (API), and the API functions is callaed from StarBasic.
Yes - it's my fault - i used the MS Office terminology.
Zizi64 wrote:You need get the applied Page Style. The Page Style has Footer and Header properties/objects. You can get them by API functions.
Thanks - i will try
Zizi64 wrote:Do you want to insert a picture into the footer of a Sheet of a Calc document, or into the footer of a Writer document?
a Sheet of a Calc document
LibreOffice 5.1.4.2 on Windows 7
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

FJCC wrote:Here is a quick example but as Tibor says, you have to study the API to do understand the code.
Thank you - i try to run this code and get the error "runtime error basic object is not set" at this line:

Code: Select all

GrphObj.GraphicURL = GrphURL
LibreOffice 5.1.4.2 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How to insert an image into footer with Visual Basic?

Post by RoryOF »

Try dimensioning GrphObj as either Variant or Object. If you are programming in OO's macro language you should be able to understand the error messages.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to insert an image into footer with Visual Basic?

Post by Villeroy »

It's quite obvious that you are not a programmer. An office suite is a software tool that allows you to create amazing documents without being a programmer. Working with templates and styles is by far more efficient and painless than generating documents by means of scripting code.
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
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to insert an image into footer with Visual Basic?

Post by FJCC »

Sorry, my example was for a Writer document. I'll take a look at doing it in Calc.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to insert an image into footer with Visual Basic?

Post by FJCC »

A Calc example:

Code: Select all

GrphURL = convertToURL("c:\users\fjcc\desktop\Image.jpg")
oStyleFamilies = ThisComponent.getStyleFamilies()
oObj1 = oStyleFamilies.getByName("PageStyles")
oObj2 = oObj1.getByName("Default")
oObj2.FooterBackGraphicURL = GrphURL
oObj2.FooterBackGraphicLocation = com.sun.star.style.GraphicLocation.LEFT_MIDDLE
The possible values for FooterBackGraphicLocation are here
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

FJCC wrote:A Calc example:
I run this code - there are no errors, but nothing happens - there is no image in footer.

I modified slightly your code in that way:

Code: Select all

GrphURL = convertToURL("e:\_\123.jpg")
oSimpleFileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
if oSimpleFileAccess.exists(GrphURL)= False then
	MsgBox ("URL does not exist")
else
	MsgBox ("URL exists")
end if
oStyleFamilies = ThisComponent.getStyleFamilies()
oObj1 = oStyleFamilies.getByName("PageStyles")
oObj2 = oObj1.getByName("Default")
oObj2.FooterBackGraphicURL = GrphURL
oObj2.FooterBackGraphicLocation = com.sun.star.style.GraphicLocation.LEFT_BOTTOM
It shows message "URL exists" - the image file URL has been set correctly
LibreOffice 5.1.4.2 on Windows 7
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to insert an image into footer with Visual Basic?

Post by FJCC »

Please confirm that you are looking at the document using File -> Page Preview to confirm that the image is not in the footer. The macro produces no visible change in the normal document view.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

FJCC wrote:Please confirm that you are looking at the document using File -> Page Preview to confirm that the image is not in the footer. The macro produces no visible change in the normal document view.
Yes, i can confirm that - i know that image in footer is only visible in File -> Page Preview mode - when i insert an image in footer by manual manipulations - there is an image in Page Preview in the footer, but when i run your code - there is no image in the footer in Page Preview - but there are some changes in sheet - because little green star appears on the Save button.
LibreOffice 5.1.4.2 on Windows 7
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to insert an image into footer with Visual Basic?

Post by FJCC »

I tried your code after changing only the file name and it worked. Sorry, I can't think of why the code would work for me and not for you other than that there is a problem with LibreOffice. You could try installing OpenOffice just to check the code.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to insert an image into footer with Visual Basic?

Post by Zizi64 »

I just tried the modified code (with my picture link) and it works in my AOO 4.1.2 and LO 4.4.7. and LOportable5.1.4.

Maybe:
- your footer is switched off.
- your footer is too narrow (low) to display the whole picture and it can show only a white part of the picture.
- you are using different page style than the 'Default' page style. (This macro will modify the 'Default' page style only but will not modify the other existing/applied page style/s/.)

Note:
The picture seems linked, but not embedded. The .ods file (renamed it to .zip extension) have not contain any picture file after running the macro.
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.
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

Zizi64 wrote:- you are using different page style than the 'Default' page style. (This macro will modify the 'Default' page style only but will not modify the other existing/applied page style/s/.)
Yes - my page style was not the 'Default' - i changed it to the 'Default' and now the code works.

One more question:
Zizi64 wrote:The picture seems linked, but not embedded. The .ods file (renamed it to .zip extension) have not contain any picture file after running the macro.
How to make picture embedded? How to uncheck this checkbox programmatically:
Image

I need it because when i check this checkbox - the picture is small in Page Preview:
Image

And when i uncheck it - the picture is in the original size:
Image
Last edited by vtssi on Thu Jul 21, 2016 3:25 pm, edited 1 time in total.
LibreOffice 5.1.4.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to insert an image into footer with Visual Basic?

Post by Zizi64 »

One more question:

Zizi64 wrote:
The picture seems linked, but not embedded. The .ods file (renamed it to .zip extension) have not contain any picture file after running the macro.


How to make picture embedded? How to uncheck this checkbox programmatically:
You can load, resize the pictures by API functions. See Andrew Pitonyak's books, and the API descriptions.
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.
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

I searched this book by the word "unlinked graphic" but nothing found.
LibreOffice 5.1.4.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to insert an image into footer with Visual Basic?

Post by Zizi64 »

http://www.pitonyak.org/oo.php
search in the 'OpenOffice.org Macro document':
"Loading/Inserting an image into your document"

and see: OpenOffice.org Macros Explained.odt
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.
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

I found exactly what i need in this link:
https://bugs.documentfoundation.org/sho ... i?id=33395

But i don't understand this answer:
Using the Graphic object service will allow you to create an XGraphicObject that is associated with a 'vnd.sun.star.GraphicObject' scheme URL. Setting the BackGraphicURL, HeaderBackGraphicURL, FooterBackGraphicURL with such and embedded graphic url should do what you require
I found nothing about XGraphicObject or vnd.sun.star.GraphicObject in OpenOffice.org Macros Explained.odt
This is what i tried to do:

Code: Select all

GrphURL = convertToURL("e:\_\123.jpg")
oStyleFamilies = ThisComponent.getStyleFamilies()
oObj1 = oStyleFamilies.getByName("PageStyles")
oObj2 = oObj1.getByName("Default")
oImagen_obj = ThisComponent.createInstance("com.sun.star.drawing.GraphicObjectShape")
oImagen_obj.GraphicURL = GrphURL
XGraphicObj = ThisComponent.createInstance("com.sun.star.graphic.XGraphic")
XGraphicObj = oImagen_obj.Graphic
MsgBox(XGraphicObj.UniqueID)
oObj2.FooterBackGraphicURL = XGraphicObj.UniqueID
oObj2.FooterBackGraphicLocation = com.sun.star.style.GraphicLocation.LEFT_BOTTOM
But there is an error on the line:

Code: Select all

XGraphicObj = oImagen_obj.Graphic
How can i get this string:

Code: Select all

vnd.sun.star.GraphicObject:1000000000000800000006008E87321C
from com.sun.star.drawing.GraphicObjectShape object?
LibreOffice 5.1.4.2 on Windows 7
vtssi
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

Re: How to insert an image into footer with Visual Basic?

Post by vtssi »

I've done it. The problem is solved.
Thank you FJCC and Zizi64 very much.

This is a correct code:

Code: Select all

GrphURL = convertToURL("e:\_\123.jpg")
oShape = ThisComponent.createInstance("com.sun.star.drawing.GraphicObjectShape")
oDP = ThisComponent.DrawPages.getByIndex(0)
oDP.add(oShape)
Dim oProps(1) as new com.sun.star.beans.PropertyValue
oProps(0).Name = "URL"
oProps(0).Value = GrphURL
oProps(1).Name = "AsLink"
oProps(1).Value = false
oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
oShape.Graphic = oProvider.queryGraphic(oProps())
oStyleFamilies = ThisComponent.getStyleFamilies()
oObj1 = oStyleFamilies.getByName("PageStyles")
oObj2 = oObj1.getByName("Default")
oObj2.FooterOn = True
oObj2.FooterIsShared = True
oObj2.FooterHeight = 16000
oObj2.FooterBackGraphicURL = oShape.GraphicURL
oObj2.FooterBackGraphicLocation = com.sun.star.style.GraphicLocation.LEFT_BOTTOM
LibreOffice 5.1.4.2 on Windows 7
Post Reply