I have successfully programmed automation of Microsoft Word with Java and SWT. Now I need to do the same for Open Office / Libre Office writter.
In case of MS Word I used Word.Application, something like that:
...
wordSite = new OleClientSite(frame, SWT.NONE, "Word.Application");
wordAutomation = new OleAutomation(wordSite);
...
wordautomation.getIDsOfNames ....
I believe this should be done in similar way with Open Office. Which object(s) should I use? And how? Thank you
EDIT1: I tried to avoid creating long question, but since my question is downvoted I will try to explain it in more details:
- My long term need is to open and automate the open office document via Java applet
- I tried UNO and Office bean, but both seem to be useless in my case (path and environment settings dependency), which are not accepted when you launch something from web browser
- I asked for objects due to my previous experience with Microsoft Word. When I used Word.Document I could open the document but automation didn't work. When I opened the document via Word.Application then automation did work, but it didn't behave like ole object which I like and would like to use.
Currently my code (below) does not let me use automation features, it works like Word.Document.
OODocument(String systemName, String fileName) {
this.fileName = fileName;
display = new Display();
shell = new Shell(display);
shell.setSize(1000, 700);
shell.setLayout(new FillLayout());
oleFrame = new OleFrame(shell, SWT.NONE);
oleClientSite = new OleClientSite(oleFrame, SWT.NONE, new File(fileName));
oleClientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
addMenu(oleFrame, systemName);
shell.open();
OleAutomation oleDocument = new OleAutomation(oleClientSite);
infoLabel.setVisible(false);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
oleDocument.dispose();
display.dispose();
}
That's way I asked the question: "which objects shall I use". I would like to find object(s) similar to Word.Application. Shall it be com.sun.star.frame.Desktop? If so I didn't succeed using it yet.
I thought that one might programmed OOO access via OLe in the past and could share some ideas or code.
Thank you.
EDIT 2 Below is the code from OpenOffice SDK, written in vbs. If somebody could help me converting this to JAVA and SWT it would be the solution:
'The service manager is always the starting point
'If there is no office running then an office is started up
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
'Create the CoreReflection service that is later used to create structs
Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
'Create the Desktop
Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")
'Open a new empty writer document
Dim args()
Set objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
'Create a text object
Set objText= objDocument.getText
'Create a cursor object
Set objCursor= objText.createTextCursor
'Inserting some Text
objText.insertString objCursor, "The first line in the newly created text document." & vbLf, 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
Set objTable= objDocument.createInstance( "com.sun.star.text.TextTable")
objTable.initialize 4, 4
'Insert the table
objText.insertTextContent objCursor, objTable, false
'Get first row
Set objRows= objTable.getRows
Set 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" & vbLf, false
'Create a paragraph break ( ControlCharacter::PARAGRAPH_BREAK).
objText.insertControlCharacter objCursor, 0, false
'Create a TextFrame.
Set objTextFrame= objDocument.createInstance("com.sun.star.text.TextFrame")
'Create a Size struct.
Set objSize= createStruct("com.sun.star.awt.Size")
objSize.Width= 15000
objSize.Height= 400
objTextFrame.setSize( objSize)
' TextContentAnchorType.AS_CHARACTER = 1
objTextFrame.setPropertyValue "AnchorType", 1
'insert the frame
objText.insertTextContent objCursor, objTextFrame, false
'Get the text object of the frame
Set objFrameText= objTextFrame.getText
'Create a cursor object
Set objFrameTextCursor= objFrameText.createTextCursor
'Inserting some Text
objFrameText.insertString objFrameTextCursor, "The first line in the newly created text frame.", _
false
objFrameText.insertString objFrameTextCursor, _
vbLf & "With this second line the height of the frame raises.", false
'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objFrameText.insertControlCharacter objCursor, 0 , false
'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 65536
objCursor.setPropertyValue "CharShadowed", false
'Insert another string
objText.insertString objCursor, " That's all for now !!", false
On Error Resume Next
If Err Then
MsgBox "An error occurred"
End If
Sub insertIntoCell( strCellName, strText, objTable)
Set objCellText= objTable.getCellByName( strCellName)
Set objCellCursor= objCellText.createTextCursor
objCellCursor.setPropertyValue "CharColor",16777215
objCellText.insertString objCellCursor, strText, false
End Sub
Function createStruct( strTypeName)
Set classSize= objCoreReflection.forName( strTypeName)
Dim aStruct
classSize.createObject aStruct
Set createStruct= aStruct
End Function
The code above, when saved to the .vbs file and started as: cscript WriterDemo.vbs launches the Open Office writter and some data is written into the doc. I can't get it working in Java though.
If I try starting this like that:
final String PROG_ID = "com.sun.star.ServiceManager";
display = new Display();
shell = new Shell(display);
frame = new OleFrame(shell, SWT.NONE);
writterSite = new OleControlSite(frame, SWT.NONE, PROG_ID);
writterAutomation = new OleAutomation(writterSite);
then I get the exception
Caught: org.eclipse.swt.SWTException
Failed to create Ole Client. result = -2147221164
org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164
at org.eclipse.swt.ole.win32.OLE.error(Unknown Source)
at org.eclipse.swt.ole.win32.OleControlSite.<init>(Unknown Source)
at office.WritterAutomation.<init>(WritterAutomation.java:39)
at office.WritterAutomation.main(WritterAutomation.java:454)
I suppose I am trying to use wrong objects or using them improperly. Thanks for your help.