<p>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?</p>

[{'answer_id': 38976, 'author': 'Peter Stuifzand', 'author_id': 1633, 'author_profile': 'https://Stackoverflow.com/users/1633', 'pm_score': 2, 'selected': False, 'text': '<p>You should take a look at <strong><a href="http://api.openoffice.org" rel="nofollow noreferrer">Apache OpenOffice API</a></strong>. A project for creating an API for Open Office. A few languages they said to support are: C++, Java, Python, CLI, StarBasic, JavaScript and OLE.</p>\n\n<p><a href="http://codesnippets.services.openoffice.org/Writer/Writer.MailMerge.snip" rel="nofollow noreferrer">Java Example of a mailmerge in OpenOffice</a>.</p>\n'}, {'answer_id': 42760, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 4, 'selected': True, 'text': '<p>I haven\'t come up with a solution I\'m really happy with but here are some notes:</p>\n\n<ul>\n<li><p>Q. What is the OO API for mail merge?</p>\n\n<p>A. <a href="http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html" rel="noreferrer">http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html</a></p></li>\n<li><p>Q. What support groups?</p>\n\n<p>A. <a href="http://user.services.openoffice.org/en/forum/viewforum.php?f=20" rel="noreferrer">http://user.services.openoffice.org/en/forum/viewforum.php?f=20</a></p></li>\n<li><p>Q. Sample code?</p>\n\n<p>A. <a href="http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&amp;t=946&amp;p=3778&amp;hilit=mail+merge#p3778" rel="noreferrer">http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&amp;t=946&amp;p=3778&amp;hilit=mail+merge#p3778</a></p>\n\n<p><a href="http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&amp;t=8088&amp;p=38017&amp;hilit=mail+merge#p38017" rel="noreferrer">http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&amp;t=8088&amp;p=38017&amp;hilit=mail+merge#p38017</a></p></li>\n<li><p>Q. Any more examples?</p>\n\n<p>A. file:///C:/Program%20Files/OpenOffice.org_2.4_SDK/examples/examples.html (comes with the SDK)</p>\n\n<p><a href="http://www.oooforum.org/forum/viewtopic.phtml?p=94970" rel="noreferrer">http://www.oooforum.org/forum/viewtopic.phtml?p=94970</a></p></li>\n<li><p>Q. How do I build the examples?</p>\n\n<p>A. e.g., for WriterDemo (C:\\Program Files\\OpenOffice.org_2.4_SDK\\examples\\CLI\\VB.NET\\WriterDemo)</p>\n\n<ol>\n<li>Add references to everything in here: C:\\Program Files\\OpenOffice.org    2.4\\program\\assembly</li>\n<li>That is cli_basetypes, cli_cppuhelper, cli_types, cli_ure</li>\n</ol></li>\n<li><p>Q. Does OO use the same separate data/document file for mail merge?</p>\n\n<p>A. It allows for a range of data sources including csv files</p></li>\n<li><p>Q. Does OO allow you to merge to all the different types (fax, email, new document printer)?</p>\n\n<p>A. You can merge to a new document, print and email</p></li>\n<li><p>Q. Can you add custom fields?</p>\n\n<p>A. Yes</p></li>\n<li><p>Q. How do you create a new document in VB.Net?</p>\n\n<p>A.</p>\n\n<pre><code>        Dim xContext As XComponentContext\n\n        xContext = Bootstrap.bootstrap()\n\n        Dim xFactory As XMultiServiceFactory\n        xFactory = DirectCast(xContext.getServiceManager(), _\n            XMultiServiceFactory)\n\n        \'Create the Desktop\n        Dim xDesktop As unoidl.com.sun.star.frame.XDesktop\n        xDesktop = DirectCast(xFactory.createInstance("com.sun.star.frame.Desktop"), _\n            unoidl.com.sun.star.frame.XDesktop)\n\n        \'Open a new empty writer document\n        Dim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader\n        xComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader)\n        Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = _\n            New unoidl.com.sun.star.beans.PropertyValue() {}\n        Dim xComponent As unoidl.com.sun.star.lang.XComponent\n        xComponent = xComponentLoader.loadComponentFromURL( _\n            "private:factory/swriter", "_blank", 0, arProps)\n        Dim xTextDocument As unoidl.com.sun.star.text.XTextDocument\n        xTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument)\n</code></pre></li>\n<li><p>Q. How do you save the document?</p>\n\n<p>A.</p>\n\n<pre><code>        Dim storer As unoidl.com.sun.star.frame.XStorable = DirectCast(xTextDocument, unoidl.com.sun.star.frame.XStorable)\n        arProps = New unoidl.com.sun.star.beans.PropertyValue() {}\n        storer.storeToURL("file:///C:/Users/me/Desktop/OpenOffice Investigation/saved doc.odt", arProps)\n</code></pre></li>\n<li><p>Q. How do you Open the document?</p>\n\n<p>A.</p>\n\n<pre><code>        Dim xComponent As unoidl.com.sun.star.lang.XComponent\n        xComponent = xComponentLoader.loadComponentFromURL( _\n            "file:///C:/Users/me/Desktop/OpenOffice Investigation/saved doc.odt", "_blank", 0, arProps)\n</code></pre></li>\n<li><p>Q. How do you initiate a mail merge in VB.Net?</p>\n\n<p>A.</p>\n\n<ol>\n<li><p>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.</p></li>\n<li><p>In VBScript:</p>\n\n<p>Set objServiceManager = WScript.CreateObject("com.sun.star.ServiceManager")</p>\n\n<p>\'Now set up a new MailMerge using the settings extracted from that doc\nSet oMailMerge = objServiceManager.createInstance("com.sun.star.text.MailMerge")</p>\n\n<p>oMailMerge.DocumentURL = "file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"\noMailMerge.DataSourceName = "adds"\noMailMerge.CommandType = 0 \' <a href="http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#CommandType" rel="noreferrer">http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#CommandType</a>\noMailMerge.Command = "adds"\noMailMerge.OutputType = 2 \' <a href="http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#OutputType" rel="noreferrer">http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#OutputType</a>\noMailMerge.execute(Array())</p></li>\n<li><p>In VB.Net (Option Strict Off)</p>\n\n<pre><code>    Dim t_OOo As Type\n    t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager")\n    Dim objServiceManager As Object\n    objServiceManager = System.Activator.CreateInstance(t_OOo)\n\n    Dim oMailMerge As Object\n    oMailMerge = t_OOo.InvokeMember("createInstance", _\n                    BindingFlags.InvokeMethod, Nothing, _\n                    objServiceManager, New [Object]() {"com.sun.star.text.MailMerge"})\n\n    \'Now set up a new MailMerge using the settings extracted from that doc\n    oMailMerge.DocumentURL = "file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"\n    oMailMerge.DataSourceName = "adds"\n    oMailMerge.CommandType = 0 \' http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#CommandType\n    oMailMerge.Command = "adds"\n    oMailMerge.OutputType = 2 \' http://api.openoffice.org/docs/common/ref/com/sun/star/text/MailMerge.html#OutputType\n    oMailMerge.execute(New [Object]() {})\n</code></pre></li>\n<li><p>The same thing but with Option Strict On (doesn\'t work)</p>\n\n<pre><code>    Dim t_OOo As Type\n    t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager")\n    Dim objServiceManager As Object\n    objServiceManager = System.Activator.CreateInstance(t_OOo)\n\n    Dim oMailMerge As Object\n    oMailMerge = t_OOo.InvokeMember("createInstance", _\n                    BindingFlags.InvokeMethod, Nothing, _\n                    objServiceManager, New [Object]() {"com.sun.star.text.MailMerge"})\n\n    \'Now set up a new MailMerge using the settings extracted from that doc\n    oMailMerge.GetType().InvokeMember("DocumentURL", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {"file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"})\n    oMailMerge.GetType().InvokeMember("DataSourceName", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {"adds"})\n    oMailMerge.GetType().InvokeMember("CommandType", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {0})\n    oMailMerge.GetType().InvokeMember("Command", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {"adds"})\n    oMailMerge.GetType().InvokeMember("OutputType", BindingFlags.SetProperty, Nothing, oMailMerge, New [Object]() {2})\n    oMailMerge.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod Or BindingFlags.IgnoreReturn, Nothing, oMailMerge, New [Object]() {}) \' this line fails with a type mismatch error\n</code></pre></li>\n</ol></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/38842', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3146/']