Page 1 of 1

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

Posted: Tue Jul 19, 2016 1:58 pm
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.

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

Posted: Tue Jul 19, 2016 2:45 pm
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

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

Posted: Tue Jul 19, 2016 4:58 pm
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)

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

Posted: Tue Jul 19, 2016 6:03 pm
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

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

Posted: Tue Jul 19, 2016 6:38 pm
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

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

Posted: Tue Jul 19, 2016 6:41 pm
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.

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

Posted: Tue Jul 19, 2016 6:44 pm
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.

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

Posted: Tue Jul 19, 2016 6:45 pm
by FJCC
Sorry, my example was for a Writer document. I'll take a look at doing it in Calc.

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

Posted: Wed Jul 20, 2016 4:38 am
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

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

Posted: Wed Jul 20, 2016 10:53 am
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

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

Posted: Wed Jul 20, 2016 4:49 pm
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.

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

Posted: Wed Jul 20, 2016 6:38 pm
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.

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

Posted: Thu Jul 21, 2016 6:09 am
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.

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

Posted: Thu Jul 21, 2016 6:57 am
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.

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

Posted: Thu Jul 21, 2016 11:28 am
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

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

Posted: Thu Jul 21, 2016 3:18 pm
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.

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

Posted: Thu Jul 21, 2016 3:29 pm
by vtssi
I searched this book by the word "unlinked graphic" but nothing found.

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

Posted: Thu Jul 21, 2016 4:06 pm
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

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

Posted: Mon Jul 25, 2016 12:10 pm
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?

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

Posted: Mon Jul 25, 2016 6:14 pm
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