How to do a simple mail merge in OpenOffice

1800 INFORMATION picture 1800 INFORMATION · Sep 2, 2008 · Viewed 8.5k times · Source

I need to do a simple mail merge in OpenOffice using C++, VBScript, VB.Net or C# via OLE or native API. Are there any good examples available?

Answer

1800 INFORMATION picture 1800 INFORMATION · Sep 4, 2008

I haven't come up with a solution I'm really happy with but here are some notes:

  • Q. What is the OO API for mail merge?

    A. http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html

  • Q. What support groups?

    A. http://user.services.openoffice.org/en/forum/viewforum.php?f=20

  • Q. Sample code?

    A. http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=946&p=3778&hilit=mail+merge#p3778

    http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=8088&p=38017&hilit=mail+merge#p38017

  • Q. Any more examples?

    A. file:///C:/Program%20Files/OpenOffice.org_2.4_SDK/examples/examples.html (comes with the SDK)

    http://www.oooforum.org/forum/viewtopic.phtml?p=94970

  • Q. How do I build the examples?

    A. e.g., for WriterDemo (C:\Program Files\OpenOffice.org_2.4_SDK\examples\CLI\VB.NET\WriterDemo)

    1. Add references to everything in here: C:\Program Files\OpenOffice.org 2.4\program\assembly
    2. That is cli_basetypes, cli_cppuhelper, cli_types, cli_ure
  • Q. Does OO use the same separate data/document file for mail merge?

    A. It allows for a range of data sources including csv files

  • Q. Does OO allow you to merge to all the different types (fax, email, new document printer)?

    A. You can merge to a new document, print and email

  • Q. Can you add custom fields?

    A. Yes

  • Q. How do you create a new document in VB.Net?

    A.

            Dim xContext As XComponentContext
    
            xContext = Bootstrap.bootstrap()
    
            Dim xFactory As XMultiServiceFactory
            xFactory = DirectCast(xContext.getServiceManager(), _
                XMultiServiceFactory)
    
            'Create the Desktop
            Dim xDesktop As unoidl.com.sun.star.frame.XDesktop
            xDesktop = DirectCast(xFactory.createInstance("com.sun.star.frame.Desktop"), _
                unoidl.com.sun.star.frame.XDesktop)
    
            'Open a new empty writer document
            Dim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader
            xComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader)
            Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = _
                New unoidl.com.sun.star.beans.PropertyValue() {}
            Dim xComponent As unoidl.com.sun.star.lang.XComponent
            xComponent = xComponentLoader.loadComponentFromURL( _
                "private:factory/swriter", "_blank", 0, arProps)
            Dim xTextDocument As unoidl.com.sun.star.text.XTextDocument
            xTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument)
    
  • Q. How do you save the document?

    A.

            Dim storer As unoidl.com.sun.star.frame.XStorable = DirectCast(xTextDocument, unoidl.com.sun.star.frame.XStorable)
            arProps = New unoidl.com.sun.star.beans.PropertyValue() {}
            storer.storeToURL("file:///C:/Users/me/Desktop/OpenOffice Investigation/saved doc.odt", arProps)
    
  • Q. How do you Open the document?

    A.

            Dim xComponent As unoidl.com.sun.star.lang.XComponent
            xComponent = xComponentLoader.loadComponentFromURL( _
                "file:///C:/Users/me/Desktop/OpenOffice Investigation/saved doc.odt", "_blank", 0, arProps)
    
  • Q. How do you initiate a mail merge in VB.Net?

    A.

    1. Don't know. This functionality is in the API reference but is missing from the IDL. We may be slightly screwed. Assuming the API was working, it looks like running a merge is fairly simple.

    2. In VBScript:

      Set objServiceManager = WScript.CreateObject("com.sun.star.ServiceManager")

      'Now set up a new MailMerge using the settings extracted from that doc Set oMailMerge = objServiceManager.createInstance("com.sun.star.text.MailMerge")

      oMailMerge.DocumentURL = "file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt" oMailMerge.DataSourceName = "adds" oMailMerge.CommandType = 0 ' http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#CommandType oMailMerge.Command = "adds" oMailMerge.OutputType = 2 ' http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#OutputType oMailMerge.execute(Array())

    3. In VB.Net (Option Strict Off)

          Dim t_OOo As Type
          t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager")
          Dim objServiceManager As Object
          objServiceManager = System.Activator.CreateInstance(t_OOo)
      
          Dim oMailMerge As Object
          oMailMerge = t_OOo.InvokeMember("createInstance", _
                          BindingFlags.InvokeMethod, Nothing, _
                          objServiceManager, New [Object]() {"com.sun.star.text.MailMerge"})
      
          'Now set up a new MailMerge using the settings extracted from that doc
          oMailMerge.DocumentURL = "file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"
          oMailMerge.DataSourceName = "adds"
          oMailMerge.CommandType = 0 ' http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#CommandType
          oMailMerge.Command = "adds"
          oMailMerge.OutputType = 2 ' http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#OutputType
          oMailMerge.execute(New [Object]() {})
      
    4. The same thing but with Option Strict On (doesn't work)

          Dim t_OOo As Type
          t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager")
          Dim objServiceManager As Object
          objServiceManager = System.Activator.CreateInstance(t_OOo)
      
          Dim oMailMerge As Object
          oMailMerge = t_OOo.InvokeMember("createInstance", _
                          BindingFlags.InvokeMethod, Nothing, _
                          objServiceManager, New [Object]() {"com.sun.star.text.MailMerge"})
      
          'Now set up a new MailMerge using the settings extracted from that doc
          oMailMerge.GetType().InvokeMember("DocumentURL", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {"file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"})
          oMailMerge.GetType().InvokeMember("DataSourceName", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {"adds"})
          oMailMerge.GetType().InvokeMember("CommandType", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {0})
          oMailMerge.GetType().InvokeMember("Command", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {"adds"})
          oMailMerge.GetType().InvokeMember("OutputType", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {2})
          oMailMerge.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod Or BindingFlags.IgnoreReturn, Nothing, oMailMerge, New [Object]() {}) ' this line fails with a type mismatch error