views:

38

answers:

2

Hello,

I would like to have several custom fields from a (server) database merged into Word Processing documents, beyond the normal "mail merge" fields. I'm not finding anything like an API out of the box that allows you to do this for custom fields. This seems like it would be pretty common in business software, but I assume that's one more reason why the Mac doesn't excel there.

If I were to write a Mac OS X/Objective-C/Cocoa app for this, would the best way to approach it be finding and replacing strings in an RTF file? Maybe in the raw RTF? Is there a better way or format to work with for this?

Any thoughts and input would be greatly appreciated.

A: 

hat there isn't a dedicated "mail merge API" isn't a detractor - it would have to be too specific to be generally useful enough to be an API. If you think about this more closely, there's no abstract-enough case to justify a dedicated, built-in API. Most people want to "merge records from a MySQL database into a Word document" or "merge records from an Oracle database into a PDF document".

All the building blocks are there to build it, though, and it's relatively simple (pseudocode):

retrieve interesting records

for each record

  load a new copy of template

  replace strings

  save modified template to a file or do whatever

  next record

Some points:

  1. Cocoa reads and writes RTF/RTFD, and Word Doc formats natively. You'll find this information in the text system documentation.
  2. "Reading fields from a database" is very general. If you don't already have the connectivity to the database sorted out (ie, you already have the merge data), you'll need to be more specific about the database (ie, what you've tried, if anything).
  3. Once you have your data and your template document, "mail merge" is basically just string replacement of a token (like $$!FIRSTNAME!$$ or some other ridiculously-unique combination). This is easily handled with NSMutableString's built-in functions and a for loop.
  4. There are RegEx frameworks out there if you need something more complicated regarding #3.
Joshua Nozzi
A: 

You might want to check out Matt Gemmell's MGTemplateEngine as it sounds like it might be what you're looking for.

Rob Keniger