« Home | Hiding CRM Elements » | CRM 5.0 Features » | Change the default entity in Lookup Window » | Multiple Column Sort in Dynamics CRM » | Display a Google Map for an Account » | ASP.NET Application Deployment Resources » | Visual Studio Error: The project file ' ' has been... » | Progress Bar in Dynamics CRM 4.0 » | Backup, Restore & Publish Dynamics CRM Customizati... » | 'The application is already precompiled' error »

Dynamic generation of Word(.DOCX) file

Just came across a problem few days back when creating dynamic word(.docx) file. After creation, the com object was still sitting there in the process list, which gave me some weird messages everytime and it always creates the new instance of the word object. I tried a lot and atlast found this solution in MSDN. We need to release the com object finally after completion of our work. Use the below function in your code and call this after you have completed the dynamic generation process.


private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch { }
finally
{
o = null;
}
}


Microsoft.Office.Interop.Word.ApplicationClass oWord = new Microsoft.Office.Interop.Word.ApplicationClass();

*****
Your logic for dynamic generation of word file(in my case)
*****

NAR(oWord);

You are done. Now it should work as expected.

Happy coding..

Labels: , , , , , , , , ,

Post a Comment