OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

API - Unable to automate datasource creation in VB.Net

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
jaffa13
Newbie
Newbie


Joined: 02 Mar 2010
Posts: 3

PostPosted: Tue Mar 02, 2010 6:32 am    Post subject: API - Unable to automate datasource creation in VB.Net Reply with quote

Hello,

I hope someone would be able to help me or at least point me in the right direction. I am trying to automate the creation of a new datasource from a exported csv file, this datasource is to be later used as part of a mailmerge. I have managed to connect to OpenOffice and register a new datasource however the datasource appears blank in the GUI and no table structure can be seen, is it possible that the csv file is incorrectly linked to the odb?

My system configuration is as follows:
Windows XP service pack 3
Visual Studio 2005: VB.Net
OpenOffice 3.1

Here is a copy of the code to create the datasource and a list of the API DLL's added to my solution.

cli_basetypes
cli_cppuhelper
cli_oootypes
cli_uno
cli_ure
cli_uretypes

Code:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CreateDataSource()
 End Sub

Public Function PathConverter(ByVal file As String) As String
        file = file.Replace("\", "/")
        Return "file:///" + file
End Function

Private Sub CreateDataSource()
        Dim OO_ComponentContext As XComponentContext
        Dim OO_ServiceManager As XMultiComponentFactory
        Dim OO_Desktop As XComponentLoader
        Dim OO_DS_PropVal(0) As PropertyValue
        Dim filename As String
        Dim file As String

        file = "QuoteLetter"
        filename = "C:\merge\" & file & ".csv"
        filename = PathConverter(filename)

        OO_ComponentContext = uno.util.Bootstrap.bootstrap()
        OO_ServiceManager = OO_ComponentContext.getServiceManager()
        OO_Desktop = OO_ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", OO_ComponentContext)
     
        Dim OO_DatabaseContext As XSingleServiceFactory
        Dim OO_DataSource As Object
        OO_DatabaseContext = OO_ServiceManager.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", OO_ComponentContext)
        OO_DataSource = OO_DatabaseContext.createInstance()

        '   Create DatabaseContext Name Access Interface
        '   Used to enumerate/check existance of registered datasources
        Dim OO_DC_XNameAccess As XNameAccess = DirectCast(OO_DatabaseContext, XNameAccess)

        '   Create DatabaseContext Naming Service Interface
        '   Used to register new datasources
        Dim OO_DC_XNamingService As XNamingService = DirectCast(OO_DatabaseContext, XNamingService)

        '   Check to see if datasource already exists, if so remove it
        If OO_DC_XNameAccess.hasByName(file) Then
            OO_DC_XNamingService.revokeObject(file)
        End If

        '   Create a DocumentDataSource
        Dim OO_DS_XDocumentDataSource As XDocumentDataSource = DirectCast(OO_DataSource, XDocumentDataSource)

        '   Create DataSource Storable Interface
        '   Used to encapsulate DS as a storable object
        Dim OO_DS_XStorable As XStorable = DirectCast(OO_DS_XDocumentDataSource.DatabaseDocument, XStorable)

        Dim storeprops(0) As PropertyValue
        storeprops(0) = New PropertyValue

        Try
            OO_DS_XStorable.storeAsURL("C:\merge\QuoteLetter.odb", storeprops)
        Catch ex As unoidl.com.sun.star.io.IOException
            MsgBox("error")
        End Try

        OO_DC_XNamingService.registerObject(file, OO_DataSource)

        '   Create new DataSource properties
        Dim OO_DS_PropSet As XPropertySet = DirectCast(OO_DataSource, XPropertySet)
        Dim setting As uno.Any

        setting = New uno.Any("sdbc:flat:" + filename)
        OO_DS_PropSet.setPropertyValue("URL", setting)
        setting = New uno.Any("")
        OO_DS_PropSet.setPropertyValue("User", setting)
        OO_DS_PropSet.setPropertyValue("Password", setting)
        setting = New uno.Any(False)
        OO_DS_PropSet.setPropertyValue("IsPasswordRequired", setting)

        OO_DS_XStorable.store()
    End Sub



Many thanks in advance
Back to top
View user's profile Send private message MSN Messenger
jaffa13
Newbie
Newbie


Joined: 02 Mar 2010
Posts: 3

PostPosted: Thu Mar 11, 2010 5:03 am    Post subject: Reply with quote

I have done some more research and have noticed a lot of mail merge datasource creation examples reference the "Info" property, which I had not included within my code.

I have tried to replicate this in VB.Net but the code always errors with either invalid cast or illegal argument exceptions.

It seems the setPropertyValue method takes two arguments name as a string and value as any. The Info property takes a sequence of propertyvalues, which I can create but not cast to the type of uno.Any thus causing the cast exception.

I have also tried creating a VB array of objects each object contains a single PropertyValue, however this then causes the illegal argument exception, after investigating the exception it does not indicate which argument was causing the error nor is anything returned in the exception message.

Can anyone tell me how to successfully set the Info property on the datasource using VB.Net?
Back to top
View user's profile Send private message MSN Messenger
relaX
Newbie
Newbie


Joined: 18 Mar 2010
Posts: 2
Location: Munich, Germany

PostPosted: Thu Mar 18, 2010 3:03 am    Post subject: Reply with quote

Hallo *,

I have exactly the same concept and the problem. I can't see any table in the Datasource and when I try to set the "Info" Property, than i get also an IllegalArgumentException.
The table is only referenced in the datasource, when i remove the file extension ".csv". And normally i must set this information (and other like the field delimiter or encoding) with the "Info" property.
Is there any idea or solution? Or is it a new bug in OpenOffice 3.x?

My system configuration:
- Windows XP SP 3 (German)
- OpenOffice.org 3.2
- PUNO 0.6

Best regards,
Mark
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
relaX
Newbie
Newbie


Joined: 18 Mar 2010
Posts: 2
Location: Munich, Germany

PostPosted: Thu Mar 18, 2010 4:21 am    Post subject: Reply with quote

I've found a solution in the mailing list:

I set the property now directly on the object, instead of setPropertyValue() .

e.g.
Doesn't work:
Code:
object.setPropertyValue("info", props);

Work:
Code:
object.info = props;
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
jaffa13
Newbie
Newbie


Joined: 02 Mar 2010
Posts: 3

PostPosted: Fri Mar 19, 2010 1:26 am    Post subject: Reply with quote

From the code you have posted I am assuming that your datasource created from the databasecontext.CeateInstance call is assigned to a variable of type object.

However I am assigning the result to a type of com.sun.star.sdbc.XDataSource, this prevents me from accessing the properties of the datasource, hence I cast the object to its XPropertySet interface so I can use the setproperty function to access the objects properties obviously except the "Info" property set.

Can you advise me on this any further
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group