1

I have following code block which is working perfectly for OpenOffice SDK to automate Mail merge functionality.

    Public Function runQueryOnDataSource(ByVal nameOfdDtaource As String, ByVal query As String) As Boolean
    strLog = strLog + vbCrLf + Now.ToString() + ": runQueryOnDataSource nameOfdDtaource:" + nameOfdDtaource + ",query:" + query + "-Started"
    Dim oDB As Object, oBase As Object
    Dim oStatement As Object
    Dim rSQL As String
    Dim oRequete As Object
    Dim oServiceManager As Object, CreateUnoService As Object
    Try
        'Creation instance Open office
        oServiceManager = CreateObject("com.sun.star.ServiceManager")
        CreateUnoService = oServiceManager.createInstance("com.sun.star.sdb.DatabaseContext")
        mxMSFactory = (uno.util.Bootstrap.bootstrap()).getServiceManager()
        oDB = CreateUnoService.getByName(nameOfdDtaource) 'oDB=XDataSource
        'Connection
        oBase = oDB.getConnection("", "")   'oBase=XConnection
        oStatement = oBase.createStatement  'XStatement
        'rSQL = "SELECT * FROM ""26_MailMergeResult_DEMO"
        rSQL = query
        oRequete = oStatement.execute(rSQL)
        Return True
    Catch ex As Exception
        strLog = strLog + vbCrLf + Now.ToString() + ": Exception" + ex.ToString()
        Throw ex
    Finally
        oDB = Nothing
        oBase.Close()
        oBase.Dispose()
    End Try
    strLog = strLog + vbCrLf + Now.ToString() + ": runQueryOnDataSource-Finished"
    Return True
End Function

Above code is used to insert data into the DataSource already registered with libre office.But Now when I try to use it, line oServiceManager = CreateObject("com.sun.star.ServiceManager") generates error "Error Creating ActiveX object". Do anyone have idea, How do I fix this.

Mayur Bari
  • 23
  • 8
  • I have done following modification to the above code.`m_xContext = uno.util.Bootstrap.bootstrap() mxMSFactory = DirectCast(m_xContext.getServiceManager(), XMultiServiceFactory) databaseContext = mxMSFactory.createInstance("com.sun.star.sdb.DatabaseContext‌​") Dim databaseNames As container.XNameAccess = DirectCast(databaseContext, container.XNameAccess) oDB = databaseNames.getByName(nameOfdDtaource) ` . Now I'm getting run-time error "Public member 'getConnection' on type 'Any' not found." – Mayur Bari Nov 10 '17 at 06:06
  • An If I change `oDB = databaseNames.getByName(nameOfdDtaource) to oDB=DirectCast(databaseNames.getByName(nameOfdDtaource),XDat‌​aSource)` I get "Value of type 'Any' cannot be converted to 'XDataSource'". – Mayur Bari Nov 10 '17 at 06:08

1 Answers1

1

This code does not look right, so I'm surprised it ever worked. In other examples, the bootstrap() line always goes first. Then use that service manager instead of a separate oServiceManager variable.

For example, see the Java code at https://www.openoffice.org/udk/common/man/spec/transparentofficecomponents.html.

EDIT:

You're almost there. The getByName() method returns uno.Any, which has a property called Value that DirectCast can use.

Dim oDB As XDataSource
Dim oBase As XConnection = Nothing
Dim xContext As XComponentContext = uno.util.Bootstrap.bootstrap()
Dim xMSFactory As XMultiServiceFactory = DirectCast(
    xContext.getServiceManager(), XMultiServiceFactory)
Dim xNameAccess As XNameAccess = DirectCast(
    xMSFactory.createInstance("com.sun.star.sdb.DatabaseContext"), XNameAccess)
oDB = DirectCast(xNameAccess.getByName("Bibliography").Value, XDataSource)
oBase = DirectCast(oDB.getConnection("", ""), XConnection)
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • I want to implement this functionaloity for 64 bit Libre Office.I have downloaded LibreOffice_5.4_SDK which contains cli_cppuhelper Version=0.0.0.0. When I execute, I'm getting following error Could not load file or assembly 'cli_cppuhelper, Version=0.0.0.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e' or one of its dependencies. An attempt was made to load a program with an incorrect format. – Mayur Bari Feb 02 '18 at 09:30
  • That should be a new question. Be sure to include tags for VB.NET and CLR, as I am not necessarily an expert on those topics. Also, mention downloaded version specifics - are both the SDK and the app 64-bit? Mention relevant Visual Studio settings as well. – Jim K Feb 02 '18 at 13:34
  • Thanks for information but I already posted a question [link](https://stackoverflow.com/questions/48315088/automate-mail-merge-functionality-of-libre-office-64bit-using-net) and after not getting response for a week I thought to add comment here so that if you know something it would be of great help to me. – Mayur Bari Feb 05 '18 at 04:07