开发者

Com Exception: Word was unable to read this document. It may be corrupt

I have a web app that takes some client info and produces a letter for each client. Everything works good in my test environment, but on the production server I get an error that says the file is corrupt. I can open the .dotx file in word just fine on the server but not via code. Please help. Here is my code:

Object oMissing = System.Reflection.Missing.Value;

            Object oTrue = true;
            Object oFalse = false;

            Word.Application oWord = new Word.Application();
            Word.Document oWordDoc = new Word.Document();


            oWord.Visible = false;

            Object oTemplatePath = Request.PhysicalApplicationPath + "letters\\" + letter.letter_data; //samplehollisletter.dotx";

            oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

            foreach (Word.Field myMergeField in oWordDoc.Fields)
            {

                iTotalFields++;
                Word.Range rngFieldCode = myMergeField.Code;
                String fieldText = rngFieldCode.Text;

                if (fieldText.StartsWith(" MERGEFIELD"))
                {

                    Int32 endMerge = fieldText.IndexOf("\\");
                    Int32 fieldNameLength = fieldText.Length - endMerge;
                    String fieldName = fieldText.Substring(11, endMerge - 11);

                    fieldName = fieldName.Trim();

                    if (fieldName == "letter_title")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.letter_title);
                    }
                    if (fieldName == "account_id")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.account_id);
                    }

                    if (fieldName == "address")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.PEOPLE.home_address + "\r\n" + acct.PEOPLE.home_city + ", " + acct.PEOPLE.home_state + " " + acct.PEOPLE.home_zip);
                    }

                    if (fieldName == "greeting_title")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(acct.greeting_title);
            开发者_运维百科        }

                    if (fieldName == "service_name")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText((acct.SERVICEs.FirstOrDefault()).service_name);
                    }

                    if (fieldName == "service_date")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText((acct.SERVICEs.FirstOrDefault()).service_date.ToString());
                    }


                }
            }
            oWordDoc.PrintOut();
            oWordDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
            Thread.Sleep(2000);
            oWord.Quit();

The Error is:

Server Error in '/Tracker2' Application. 

Word was unable to read this document. It may be corrupt. Try one or more of the following: * Open and Repair the file. * Open the file with the Text Recovery converter. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Word was unable to read this document. It may be corrupt. Try one or more of the following: * Open and Repair the file. * Open the file with the Text Recovery converter.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[COMException (0x800a141f): Word was unable to read this document. It may be corrupt. Try one or more of the following: * Open and Repair the file. * Open the file with the Text Recovery converter.] Microsoft.Office.Interop.Word.Documents.Add(Object& Template, Object& NewTemplate, Object& DocumentType, Object& Visible) +0 Tracker.RunLetter2.Button1_Click(Object sender, EventArgs e) in C:\Users\Ethan\Documents\Visual Studio 2010\Projects\EstateTracker\Tracker\RunLetter2.aspx.cs:52 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691


I know it is time that this item has been opened, but as I had not found the answer anywhere, following solution for those who need it.

1 - In IIS, the application pool used by the application, change the Identity attribute to LocalSystem

2 - Create a folder called Desktop within the following directories on the server: C:\Windows\System32\config\systemprofile and C:\Windows\SysWOW64\config\systemprofile

After that, give full permission for these two folders to the user group IIS: IIS_IUSRS

This will cause this user to have a "desktop" to work, thereby achieving the IIS Word use the library.

I rough it helps someone.


I have discovered that the issue stems from permissions of a WCF call using BasicHTTPBinding endpoints. When a call is made using this type of endpoint, the service assumes the use of an IIS account which does not have a desktop to open word. This is a requirement of the account to automate word. Even when you have a service, that launches a windows application, that launches word, the entire set of events will be given the privilage of the original WCF call and will result in this error.

My solution, while not great, nor what I really want, does work for the time being. I created a Queue table in the database. I then have the web app insert a request for a task to be completed. then on the server I have a standalone application that checks the queue for requests every 60 seconds and processes the request. Its not the best method boe like I said, it does work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜