April 28, 2006

Salesforce.com Mail Merge, Part V: Where Movie Sequels Fear to Tread…

Filed under: Howto, Code, Salesforce.com, Mail Merge — heretic @ 7:20 pm

In our 5th part of our 4,323,412 part series on building our own Mail Merge tool for Salesforce.com we take a look at downloading the mail merge template file, and getting Word to perform the merge.

Lets tackle the downloading of our merge template. We’ll presume at this point you have the Salesforce.com Id of the template you are going to use. Just as we used the Apache Commons HTTPClient in the last part to download the Merge XML document we will do almost exactly the same to download the templace document. The URL to use is http://{instance}.salesforce.com/servlet/servlet.SForceMailMerge?id={templateId}. This time instead of taking the result and sending to to be parsed into XML, write it to a file. (I’ll show code for this in the wrapup where I provide working code for the merge applet.)

Here’s the fun part: From Java how the hell do we tell MS Word to do the merge?! And do it from a signed Java Applet in a browser!

COM: Nope. Calling winword.exe directly: Nope, can’t trigger the merge that way. Batch file: Nope, no scripting the merge again. Arrgh. Pass the headache medicine.

Before you give up, there is a way do do this. There is a way to access the COM event model from WinWord (or any other COM-aware application for that matter) using a script. It’s available in Windows 2000+ and it’s called wscript.

By writing out a VBScript file and calling wscript to execute it you can script hte mail merge into Word. Of course there’s no direct control of it in Java, but more of a fire-and-forget kind of thing. But it works!

We create a function called “writeVBS()” that takes 2 parameters, the Java.IO.File object for the CSV file we wrote before and the MailMergeTemplate above. We create a new file for our VBScript and start writing to it.

Dim objWord, objDoc
Set objWord = CreateObject(”Word.Application”)
objWord.DisplayAlerts = wdAlertsNone
Set objDoc = objWord.Documents.Open(”templateFile”)
objDoc.MailMerge.OpenDataSource “csvFile”
objDoc.MailMerge.Execute False
objDoc.Close False
objWord.Visible = True
objWord.DisplayAlerts = wdAlertsAll

And Vola! One merged doucument!

There are quite a few things I left out here. Alot of the java code to get and write this file, and how to execute it. (Runtime.getRuntime().exec(”wscript “+vbsFile)) And there’s one gotcha in the VBScript above. Word gets pissy if you have a merge field defined that doesn’t exist.

Next week I’ll be posting my full working code and the solution to that problem. It’s just not pretty. :(

License

This work is published under a Creative Commons Attribution-ShareAlike 2.5 License.

2 Comments

  1. I haven’t implemented it yet but the whole solution looks promising. The only concern I have for now is the loop user->sfdc web page-Java-VBScript-Word docs->user. I wonder if an on-demand merge hosted by an in-house web app would be more difficult than a software solution doing web page-> .NET plugin with sfdc Office API -> Word that would require to use IE only though, and possible problems related to plugins and Word template files management.
    What is your favorite way of implementation?

    Comment by Pierre — May 30, 2006 @ 2:53 pm

  2. Hello
    I used the script.. but how can I save the resultdoc like SaveAs when I add

    objDoc.SaveAs “myletter.doc”

    A error come

    Comment by Mican — January 10, 2007 @ 6:09 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.