邮件使用.net与Libre Office合并

时间:2017-11-08 13:13:32

标签: libreoffice libreoffice-writer

我有以下代码块,它可以完美地用于OpenOffice SDK以自动化邮件合并功能。

    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

上面的代码用于将数据插入已经在libre office注册的DataSource。但是当我尝试使用它时,行oServiceManager = CreateObject("com.sun.star.ServiceManager")生成错误"错误创建ActiveX对象"。 有谁有想法,我该如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

这段代码看起来不对,所以我很惊讶它曾经工作过。在其他示例中,bootstrap()行总是先行。然后使用该服务管理器而不是单独的oServiceManager变量。

例如,请参阅https://www.openoffice.org/udk/common/man/spec/transparentofficecomponents.html上的Java代码。

修改

你快到了。 getByName()方法返回uno.Any,其中包含Value可以使用的DirectCast属性。

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)