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

The Application Programming Interface and the OASIS Open Document Format

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

Postby vtssi » Tue Jul 19, 2016 1:58 pm

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   Expand viewCollapse view
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
vtssi
 
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

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

Postby Zizi64 » Tue Jul 19, 2016 2:45 pm

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8540
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby FJCC » Tue Jul 19, 2016 4:58 pm

Here is a quick example but as Tibor says, you have to study the API to do understand the code.
Code: Select all   Expand viewCollapse view
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)
Windows 10 and Linux Mint, since 2017
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: 7385
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby vtssi » Tue Jul 19, 2016 6:03 pm

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?

Postby vtssi » Tue Jul 19, 2016 6:38 pm

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   Expand viewCollapse view
GrphObj.GraphicURL = GrphURL
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?

Postby RoryOF » Tue Jul 19, 2016 6:41 pm

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.7 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29896
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Postby Villeroy » Tue Jul 19, 2016 6:44 pm

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27380
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby FJCC » Tue Jul 19, 2016 6:45 pm

Sorry, my example was for a Writer document. I'll take a look at doing it in Calc.
Windows 10 and Linux Mint, since 2017
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: 7385
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby FJCC » Wed Jul 20, 2016 4:38 am

A Calc example:
Code: Select all   Expand viewCollapse view
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
Windows 10 and Linux Mint, since 2017
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: 7385
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby vtssi » Wed Jul 20, 2016 10:53 am

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   Expand viewCollapse view
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
vtssi
 
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

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

Postby FJCC » Wed Jul 20, 2016 4:49 pm

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.
Windows 10 and Linux Mint, since 2017
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: 7385
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby vtssi » Wed Jul 20, 2016 6:38 pm

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
vtssi
 
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

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

Postby FJCC » Thu Jul 21, 2016 6:09 am

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.
Windows 10 and Linux Mint, since 2017
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: 7385
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby Zizi64 » Thu Jul 21, 2016 6:57 am

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8540
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby vtssi » Thu Jul 21, 2016 11:28 am

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
vtssi
 
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm

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

Postby Zizi64 » Thu Jul 21, 2016 3:18 pm

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8540
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby vtssi » Thu Jul 21, 2016 3:29 pm

I searched this book by the word "unlinked graphic" but nothing found.
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?

Postby Zizi64 » Thu Jul 21, 2016 4:06 pm

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8540
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby vtssi » Mon Jul 25, 2016 12:10 pm

I found exactly what i need in this link:
https://bugs.documentfoundation.org/show_bug.cgi?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   Expand viewCollapse view
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   Expand viewCollapse view
XGraphicObj = oImagen_obj.Graphic

How can i get this string:
Code: Select all   Expand viewCollapse view
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?

Postby vtssi » Mon Jul 25, 2016 6:14 pm

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

This is a correct code:
Code: Select all   Expand viewCollapse view
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
vtssi
 
Posts: 9
Joined: Tue Jul 19, 2016 1:51 pm


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 1 guest