Page 1 of 1

[Solved] How to use CLI UNO in MS Basic

Posted: Mon Aug 10, 2009 3:52 am
by bradley
OO wiki below recommends using CLI Uno rather than OLE Automation bridge. How?

http://wiki.services.openoffice.org/wiki/Uno/CLI

"Common Language Infrastructure (CLI) Uno, new in OpenOffice.org 2.0. Note: The Microsoft .NET framework is an implementation of this standard. (This bridge should be preferred over the OLE Automation bridge, as it offers more complete functionality.) "


I can use OO apps (calc, base) from .net (VB 2005) using the OLE Automation bridge. There is good documentation there. But I can find nothing for using CLI Uno, even though I have spent days searching. There is some stuff regarding C# and java, but nothing for VB. Any help would be greatly appreciated.

Thank you.

Re: How to use CLI UNO in MS Basic

Posted: Mon Aug 10, 2009 4:59 am
by bradley
Here is a solution - it uses Common Language Infrastructure (CLI) Uno with VB 2005 to open swriter.exe and insert some text.

This solution is a translation of C# from:
http://opendocument4all.com/content/view/68/47/

1. Start VB 2005 and create a new project - Windows application.

2. View Code.

3. Copy the following to replace all code.

Code: Select all

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports unoidl.com.sun.star.lang
Imports unoidl.com.sun.star.uno
Imports unoidl.com.sun.star.bridge
Imports unoidl.com.sun.star.frame
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Call the bootstrap method to get a new ComponentContext  
        'object. If OpenOffice isn't already started this will  
        'start it and then return the ComponentContext.  

        Dim FileName As String = "C:\odtfiles   est.odt"
        Dim localContext As unoidl.com.sun.star.uno.XComponentContext = uno.util.Bootstrap.bootstrap()
        'Get a new service manager of the MultiServiceFactory type  
        'we need this to get a desktop object and create new CLI  
        'objects.  
        Dim multiServiceFactory As unoidl.com.sun.star.lang.XMultiServiceFactory = DirectCast(localContext.getServiceManager(), unoidl.com.sun.star.lang.XMultiServiceFactory)
        'Create a new Desktop instance using our service manager  
        'Notice: We cast our desktop object to XComponent loader  
        'so that we could load or create new documents.  
        Dim componentLoader As XComponentLoader = DirectCast(multiServiceFactory.createInstance("com.sun.star.frame.Desktop"), XComponentLoader)
        'Create a new blank writer document using our component  
        'loader object.  
        'a blank writer document  
        'into a blank frame use no searchflag  
        'use no additional arguments.  
        Dim xComponent As XComponent = componentLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, New unoidl.com.sun.star.beans.PropertyValue(-1) {})
        'Cast our component to a the XText interface  
        'and write some simple text into document.  
        DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument).getText().setString("Hello I'm the first text!")
        'After we insert our text, we cast our component to XStorable  
        'to save it onto the harddisk  
        'Convert the file path into a OpenOffice path  
        'no additional arguments  
        DirectCast(xComponent, XStorable).storeToURL(PathConverter(FileName), New unoidl.com.sun.star.beans.PropertyValue(-1) {})
    End Sub
    Private Shared Function PathConverter(ByVal file As String) As String
        Try
            file = file.Replace("\", "/")
            Return "file:///" & file
        Catch ex As System.Exception
            Throw ex
        End Try
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class
3. From the VB toolbox, in design mode, add 2 command buttons. Button1 when clicked starts OOo writer. Button2 exits.

4. Close VB and Save.

5. Copy these files (they are in Program files) to your VB project folder, the one under folder with .sln & .suo

cli_basetypes.dll
cli_cppuhelper.dll
cli_oootypes.dll
cli_ure.dll
cli_uretypes.dll

6. Start VB again and open the saved project.

7. Do Project - Add reference - Browse - select all 5 files.

8. F5 to run - Press Button1 - it should start swriter.

9. The .exe is in the release folder of the VB project.