I had written a function to generate a word document by using the Office Interop Word object. My code is as shown below. It is working fine in my local system. But when i try to run it from window server 2003 it is not working. It keeps loading at Word.Document
wordDoc = wordApp.Documents.Open and doesn't do anything.
From the code which I have given in my above post, I am trying to achieve word automation under asp.net 4 and in windows server 2003 R2 Standart x64 Edition. I searched in google but I could not find any solution. I even tried the solutions from following
threads but nothing is working for me.
No it is not generating any exception. I also tried wrapping the code in a try catch statement. Still nothing happens. Is there any other alternative for my function? Could you please help me?
javedhakim
Member
88 Points
187 Posts
How to generate word document in windows server 2003?
Jun 18, 2012 08:17 AM|LINK
I had written a function to generate a word document by using the Office Interop Word object. My code is as shown below. It is working fine in my local system. But when i try to run it from window server 2003 it is not working. It keeps loading at Word.Document wordDoc = wordApp.Documents.Open and doesn't do anything.
private void GenerateEmploymentCertificate() { object Nothing = System.Reflection.Missing.Value; object format = Word.WdSaveFormat.wdFormatDocument; Word.Application wordApp = new Word.ApplicationClass(); object srcFileName = Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Templates\Employment Certificate.doc")); Word.Document wordDoc = wordApp.Documents.Open (ref srcFileName, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); try { object bookmarkDate = "Date"; wordDoc.Bookmarks.get_Item(ref bookmarkDate).Select(); wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblRequestdate.Text); object bookmarkEmployeeName = "EmployeeName"; wordDoc.Bookmarks.get_Item(ref bookmarkEmployeeName).Select(); wordApp.Selection.Text = lblEmployeeName1.Text; object bookmarkCompany = "Company"; wordDoc.Bookmarks.get_Item(ref bookmarkCompany).Select(); wordApp.Selection.Text = lblCompanyName1.Text; object bookmarkJoiningDate = "JoiningDate"; wordDoc.Bookmarks.get_Item(ref bookmarkJoiningDate).Select(); wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblJoiningDate1.Text); object bookmarkDesignation = "Designation"; wordDoc.Bookmarks.get_Item(ref bookmarkDesignation).Select(); wordApp.Selection.Text = lblDesignation1.Text; string DocName; DocName = string.Format("{0}_employment_certificate", lblRequestNo.Text); hFilename.Value = DocName; wordDoc.SaveAs(Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Letters\" + DocName + ".doc"))); } catch (Exception exp) { Session["generalError"] = null; Session["generalError"] = "There was an error at generating the letter. Please send email to unify.admin@unilever.com with this screen shot.<br /><br /><br />Request No:" + lblRequestNo.Text + "<br />Action:Submit<br />" + exp.StackTrace.ToString(); LogManager logHelper = new LogManager(Request.PhysicalApplicationPath.Trim(), "Leave System - Malaysia"); logHelper.LogError("[btnSubmit_Click - ]" + exp.Message + ".StackTrace - " + exp.StackTrace.ToString()); Response.Redirect(ResolveUrl("~/Error/ErrorHandler.aspx")); } finally { // Close wordDoc2 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing); if (wordDoc != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc); wordDoc = null; } // close wordApp wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); if (wordApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); wordApp = null; } } GC.Collect(); }teh munk
Participant
1466 Points
297 Posts
Re: How to generate word document in windows server 2003?
Jun 18, 2012 08:24 AM|LINK
Does your server have Microsoft Office installed on it?
javedhakim
Member
88 Points
187 Posts
Re: How to generate word document in windows server 2003?
Jun 18, 2012 09:04 AM|LINK
Yes. It have office 2007 installed in it. My local system also contains Office 2007.
javedhakim
Member
88 Points
187 Posts
Re: How to generate word document in windows server 2003?
Jun 18, 2012 12:50 PM|LINK
From the code which I have given in my above post, I am trying to achieve word automation under asp.net 4 and in windows server 2003 R2 Standart x64 Edition. I searched in google but I could not find any solution. I even tried the solutions from following threads but nothing is working for me.
Word Automation
Word 2007 Documents
What could be the problem and the solution to it? Could you please help me?
Thanks.
teh munk
Participant
1466 Points
297 Posts
Re: How to generate word document in windows server 2003?
Jun 18, 2012 04:26 PM|LINK
Is it generating and exception? If it isn't, have you tried wrapping the code in a try catch?
javedhakim
Member
88 Points
187 Posts
Re: How to generate word document in windows server 2003?
Jun 19, 2012 05:50 AM|LINK
No it is not generating any exception. I also tried wrapping the code in a try catch statement. Still nothing happens. Is there any other alternative for my function? Could you please help me?
teh munk
Participant
1466 Points
297 Posts
Re: How to generate word document in windows server 2003?
Jun 26, 2012 04:29 PM|LINK
Tbh you should really avoid trying to automate any office application server side as Office was not designed to be completely unattended.
Have a look at the Open XML SDK here; http://msdn.microsoft.com/en-gb/office/aa905354.aspx