Page 1 of 1

[Solved] How to use CLI UNO in MS Basic

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

"Common Language Infrastructure (CLI) Uno, new in 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

PostPosted: 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:

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   Expand viewCollapse view
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
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 = 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 
        Dim multiServiceFactory As = DirectCast(localContext.getServiceManager(),
        '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(""), 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 {})
        'Cast our component to a the XText interface 
        'and write some simple text into document. 
        DirectCast(xComponent,"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 {})
    End Sub
    Private Shared Function PathConverter(ByVal file As String) As String
            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 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


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.