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..
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: .docx, comobject, dynamic generation, dynamics, generation, microsoft, microsoft word, msdn, release object, visual studio
Post a Comment