How to Programmatically Create a Microsoft Word Document from Dynamics CRM

Recently I had a need to create a Microsoft Word document and dynamically populate it from data in CRM.  Microsoft Word can be invoked from JavaScript using the ActiveXObject .  This first example shows a very basic application of the concept.  Create a blank document , add a report title then add some body  text.  In my application I call the JavaScript from a toolbar button added with the ISV.config file.  For more information on adding buttons that call javaScript see Calling JavaScript from Microsoft CRM  Toolbar Buttons

// Start a new instance of Microsoft Word

var MsWord = new ActiveXObject(“Word.Application”);

// Create a new document


//Carriage Return


//Report Name


//Carriage Return
//Carriage Return

//Memo field from CRM containing paragraphs of text


//Show Microsoft Word

MsWord.Visible = true;


To create a new document using a Microsoft Word Template do this:


To add text to a specific Microsoft Word form field first create a Word template and add a field to it using the developer toolbar then use this code:
msWord.ActiveDocument.FormFields(“Text1”).Result = crmForm.all.new_name.value;

Obviously you can take this much further, in my application I use a AJAX call to a web service to get related CRM records.  I also lock all the fields so their content can’t be modified by the user.  What the user can do is add content to the document which may include HTML content.  The need to generate a report but then add custom content was the genesis for this feature.

Here’s a article about creating fields in Microsoft Word.  Click Here


2 thoughts on “How to Programmatically Create a Microsoft Word Document from Dynamics CRM

  1. hello mate,

    What versions of MS Word does this work on? I currently have the same kind of customization which creates a Word document in MS Word 2003.
    We are in the process of upgrading to Word 2007. Will these commands work with 2007?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s