Is it possible to write a word add-in or something

2019-09-14 18:59发布

问题:

I've a backend system which I want to use as the datasource for MS Word 2010 mailmerge. I've the idea that I should write a word add-in which provides the datasource and is communicating with my backend over webservices.

Is it generally possible to provide a custom datasource with a word add in? Is there a besser solution? Is it possible to do this in C#? Where can I found moar informations about this?

Thanks

回答1:

The types of data source that Word's out-of-the-box Mailmerge feature can use are well known - e.g.

  1. file-type data sources such as Word documents, plain text files, Excel workbooks and so on,
  2. "DDE" sources (Access, Excel, and MS Query, which in turn expects to work with ODBC connections)
  3. ODBC data sources and
  4. OLE DB data sources (these have to conform to some criteria which AFAIK are not documented, e.g. I think the provider has to implement iCommand)

To use the built-in Mailmerge, Word has to open the source, and you cannot override OpenDataSource.

There's no interface that lets you use an ADO.NET data source directly, or a disconnected recordset. If you want to do that, you have to "roll your own" mailmerge. One starting point you could consider is Eric White's series of articles - see, e.g. here .

Otherwise, you have to ensure that your data source is one of the above things. In the case of ODBC sources, you also have to use a DSN - you can't connect using a DSN-less connection string. You can avoid using a "file" using a System or Machine DSN, but it may actually be easier to distribute a file DSN than create one on the target machine.

In the case of OLE DB data sources, you have to have a "connection file" of some kind, either a .udl or .odc

A final note: as far as Word is concerned, "ODBC" is "old" and "OLE DB" is newer. But Microsoft announced some time ago that it was deprecating OLE DB in favour of ODBC. It isn't clear whether that only applies to SQL Server, nor have I seen anything that tells us what Word will support/favour in future. Meanwhile, of course, a lot more documents and data are being stored in cloud locations which may not be accessible using some ODBC/OLE DB drivers/providers.



回答2:

Yes you can do it from C# but it is more work than the way you were hoping to do it. If you could use a custom data source this would probably give you best bang-for-your-buck. If you are comfortable with programing (you mentioned C#) then you would write a small program (hopefully small) to control the mail merge process. I've seen JODReports and Docmosis do mail-merge doc-to-doc in the Java space, but I don't know if they have a C# api. If those tools look useful then you could definitely call them from scripts/command-line which might be your next fastest return on effort.
You also have the option of processing DOCX files yourself since they are zip files of XML you can literally open them up and make modifications. That's a lot more programming work though.
Good luck.