Write an OpenOffice / LibreOffice Document with use of Xojo (Windows Platform Only)

Some of you might already know it, But for those who don’t this might be useful.

A Few months ago, a question was raised if there exist an OpenOffice / LibreOffice plugin for Xojo, and the answer was No.
Since I’m not that good to write plugins for Xojo, I wonder if there was another way to approach this issue.

As my main platform is windows (2nd Linux), I did some investigation how to access LibreOffice with use of the OLEObject class.
And this is the result with use of OLEObject link: http://pbrd.co/1b2jSK6

I used following code example http://api.libreoffice.org/examples/OLE/vbscript/WriterDemo.vbs for my test; rewrote a part of it to RealStudio.

The code consist of one PushButton and one Method

PushButton1.Action

[code] Dim objServiceManager As OLEObject
Dim objCoreReflection As OLEObject
Dim objDesktop As OLEObject
Dim objDocument As OLEObject
Dim objText As OLEObject
Dim objCursor As OLEObject
Dim objTable As OLEObject
Dim objRows As OLEObject
Dim objRow As OLEObject

Dim Lf As String = chr(10) // Line Feed

'The service manager is always the starting point
'If there is no office running then an office is started up
objServiceManager = New OLEObject(“com.sun.star.ServiceManager”)

'Create the CoreReflection service that is later used to create structs
objCoreReflection = objServiceManager.createInstance(“com.sun.star.reflection.CoreReflection”)

'Create the Desktop
objDesktop = objServiceManager.createInstance(“com.sun.star.frame.Desktop”)

'Open a new empty writer document
Dim args() As variant

objDocument= objDesktop.loadComponentFromURL(“private:factory/swriter”, “_blank”, 0, args)

'Create a text object
objText= objDocument.getText

'Create a cursor object
objCursor= objText.createTextCursor

'Inserting some Text
objText.insertString (objCursor, "The first line in the newly created text document, made by RealStudio 2012.R2.1 " +Lf , false)

'Inserting a second line
objText.insertString (objCursor, “Now we’re in the second line”, false)

'Create instance of a text table with 4 columns and 4 rows
objTable= objDocument.createInstance( “com.sun.star.text.TextTable”)
objTable.initialize (4, 4)

'Insert the table
objText.insertTextContent ( objCursor, objTable, false)

'Get first row
objRows= objTable.getRows
objRow= objRows.getByIndex( 0)

'Set the table background color
objTable.setPropertyValue (“BackTransparent”, false)
objTable.setPropertyValue (“BackColor”, 13421823)

'Set a different background color for the first row
objRow.setPropertyValue (“BackTransparent”, false)
objRow.setPropertyValue (“BackColor”, 6710932)

'Fill the first table row

insertIntoCell (“A1”,“FirstColumn”, objTable)
insertIntoCell (“B1”,“SecondColumn”, objTable)
insertIntoCell (“C1”,“ThirdColumn”, objTable)
insertIntoCell (“D1”,“SUM”, objTable)

objTable.getCellByName(“A2”).setValue 22.5
objTable.getCellByName(“B2”).setValue 5615.3
objTable.getCellByName(“C2”).setValue -2315.7
objTable.getCellByName(“D2”).setFormula"sum A2:C2"

objTable.getCellByName(“A3”).setValue 21.5
objTable.getCellByName(“B3”).setValue 615.3
objTable.getCellByName(“C3”).setValue -315.7
objTable.getCellByName(“D3”).setFormula “sum A3:C3

objTable.getCellByName(“A4”).setValue 121.5
objTable.getCellByName(“B4”).setValue -615.3
objTable.getCellByName(“C4”).setValue 415.7
objTable.getCellByName(“D4”).setFormula “sum A4:C4

'Change the CharColor and add a Shadow
objCursor.setPropertyValue (“CharColor”, 255)
objCursor.setPropertyValue (“CharShadowed”, true)

'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objText.insertControlCharacter (objCursor, 0 , false)

'Inserting colored Text.
objText.insertString (objCursor, " This is a colored Text - blue with shadow" + Lf, false)

'Create a paragraph break ( ControlCharacter::PARAGRAPH_BREAK).
objText.insertControlCharacter (objCursor, 0, false)
[/code]

Sub insertIntoCell(strCellName As String, strText As String, objTable As OLEObject)

[code] Dim objCellText As OLEObject
Dim objCellCursor As OLEObject

objCellText= objTable.getCellByName( strCellName)
objCellCursor= objCellText.createTextCursor
objCellCursor.setPropertyValue (“CharColor”,16777215)
objCellText.insertString (objCellCursor, strText, false)[/code]

This example works both in windows XP and in Windows 7 with Libreoffice 4.1 and Realstudio 2012R2.1

For more informations how to access LLibreOffice SpreadSheet, Drawing or presentation you can find on following site: http://www.openoffice.org/api/TipsAndTricks/external.html

I guess this example should be enough to get going

Hello John,

This is fantastic information! The previous topic motivated me to start writing a plugin, and soon realized that plugin data was not readily available. Yes, there are a few examples, but the details in why certain commands were needed or the method behind a module was not explained. I am creating the book ‘plugins for windows’ to start.

Your example makes this plugin not required. Well done! I’ll finish the book and add this to the list of things to do.

Thanks for sharing,

Eugene

John, thanks for putting in the effort on this. I’m sure it will help a lot of people.

Well, [quote=32752:@Eugene Dakin]Your example makes this plugin not required[/quote]

Not really Eugene. The OLE approach works for windows, but an UNO based xplatform plugin can be made.

Hi Rick,

Hmm… I didn’t know that it would only work for Windows. I am not familiar with the abbreviation UNO, could you explain it to me (sorry for having to ask) :slight_smile:

Thanks for your help,

Eugene

[quote=33055:@Eugene Dakin]Hi Rick,

Hmm… I didn’t know that it would only work for Windows. I am not familiar with the abbreviation UNO, could you explain it to me (sorry for having to ask) :slight_smile:

Thanks for your help,

Eugene[/quote]

Here are some explanations about UNO (Universal Network Objects)

  1. http://wiki.openoffice.org/wiki/Uno/Article/Understanding_Uno
  2. http://www.openoffice.org/udk/
  3. http://www.linuxjournal.com/article/8550

http://wiki.openoffice.org/wiki/Documentation/DevGuide/OpenOffice.org_Developers_Guide

This link (Programming OpenOffice.or with Visual Basic) might be useful to understand the basic OOo API:

http://www.kalitech.fr/clients/doc/VB_APIOOo_en.html

Another useful document StarOffice Programmer’s Tutorial

More info about using the Boolean data type when you use LibreOffice / OpenOffice automation in Xojo: https://forum.xojo.com/43165-example-how-to-open-libreoffice-document-as-readonly-with-use-o/0

This link is also useful for this purpose: https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Automation_Bridge