Quantcast
Viewing all articles
Browse latest Browse all 11

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

MsWord.Documents.Add();

//Carriage Return

MsWord.Selection.TypeParagraph();

//Report Name

MsWord.Selection.TypeText(crmForm.all.new_name.value);

//Carriage Return
MsWord.Selection.TypeParagraph();
//Carriage Return
MsWord.Selection.TypeParagraph();

//Memo field from CRM containing paragraphs of text

MsWord.Selection.TypeText(crmForm.all.new_body.value);

//Show Microsoft Word

MsWord.Visible = true;

 

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

msWord.Documents.Add(“http://myserver:5555/documentTemplate.dotx”);

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


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 11

Trending Articles