29 April 2011

Word Mail Merge - How to merge through Javascript

Hello,

Word Mail Merge - How to merge through Javascript?

What I will describe here will work only on Internet Explorer. Because it is using an ActiveXObject which is not available in other browsers other than IE.

I know just one way to make Word Mail Merge to work cross browser and that is to implement this in code behind in C#, you can find an article about that on Microsoft's support page: http://support.microsoft.com/kb/301659.

I will probably also make a C# version of my own, if I have a good solution I will post it on the blog.

Here is a Javascript function that I have written for Javascript Word Mail Merge.
Code:

function mergeWordDoc() {
    var pathToTemplateFile = 'd:\\test\\extremedev_LabelTemplate.docx';
    var pathToDataSourceFile = 'd:\\test\\extremedev_DataSource.txt';
    try {
        var objWordApp = new ActiveXObject("Word.Application");
        objWordApp.Visible = false; // Hide the Word process

        var objWordDoc = objWordApp.Documents.Open(pathToTemplateFile, false, true); // Open Template ReadOnly

        objWordDoc.MailMerge.OpenDataSource(pathToDataSourceFile, 'wdOpenFormatAllWord');
        objWordDoc.MailMerge.Execute(); // Execute the merge
        objWordDoc.Close(false); // Close the Template without any prompts

        objWordApp.Visible = true; // show the new merged file
        objWordApp.Activate();

        // Below is only if you want to print the document and close it
        //objWordApp.PrintOut(false); // Send to printer, do not display any messages
        // Close Documents and Process
        //objWordApp.Quit(false);
        //objWordApp = null;
    }
    catch (e) {
        alert('Error Merging Label Template.');
    }
}

This function will merge the Label Template document with the datasource file.

The paths here are not passed by parameters because this is just an example or an idea, for you to have an working example and to know from where to start.

The datasource will look like this:
ColumnName1,    ColumnName2,   ...,    ColumnNameN
Row11,          Row12,         ...,    Row1N
...
RowM1,          RowM2,         ...,    RowMN

Note that the delimiter is comma - "," this is why if you will need to have comma inside a label value you will need to put that value inside double quotes like this: "My Te,st Value For extremedev, with comma inside".

The resource that helped me a lot when creating the above function is: http://msdn.microsoft.com/en-us/library/ms254954(v=Office.11).aspx
It helped me a lot because I was not blind when implementing it because I at least knew what methods and parameters exists. The link that I gave above does not contain a Javascript description for methods and parameters, but there are VB explanations and examples.

If you have some ideas or suggestions for Word Mail Merge then do not hesitate to leave a comment - this will help others.

2 comments:

Anonymous said...

Hi..
How to do a many to one relationship in datasource.
Consider datasource has below columns.
col1, col2, col3:N
Where col3:N is the multivalue column.
How to write data for col3:N? How the datasource should look like?

Thanks in advance,
BNL

Roman Gherman said...

Hi BNL,

Sorry but it was long time ago, and I haven't been working with that a long time.
Can not help you.

Regards,
Roman

Post a Comment

your thoughts are welcome:

Need more? Leave comments and subscribe to my blog.

.