Com例外:Word无法阅读此文档.它可能是腐败的

ecs*_*pot 3 asp.net

我有一个Web应用程序,它接收一些客户信息并为每个客户生成一封信.在我的测试环境中,一切都运行良好,但在生产服务器上,我收到一个错误,指出文件已损坏.我可以在服务器上打开.dotx文件,但不能通过代码打开.请帮忙.这是我的代码:

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();
Run Code Online (Sandbox Code Playgroud)

错误是:

Server Error in '/Tracker2' Application. 
Run Code Online (Sandbox Code Playgroud)

Word无法阅读此文档.它可能是腐败的.请尝试下列办法中的一个或多个:*打开并修复文件.*使用Text Recovery转换器打开文件.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.Runtime.InteropServices.COMException:Word无法读取此文档.它可能是腐败的.请尝试下列办法中的一个或多个:*打开并修复文件.*使用Text Recovery转换器打开文件.

来源错误:

在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:

[COMException(0x800a141f):Word无法读取此文档.它可能是腐败的.请尝试下列办法中的一个或多个:*打开并修复文件.*使用文本恢复转换器打开文件.] Microsoft.Office.Interop.Word.Documents.Add(对象和模板,对象和NewTemplate,对象和文档类型,对象和可见)+ 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(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+3691

小智 7

我知道现在是打开这个项目的时候了,但由于我没有在任何地方找到答案,因此需要解决方案.

1 - 在IIS中,应用程序使用的应用程序池将Identity属性更改为LocalSystem

2 - 在服务器上的以下目录中创建一个名为Desktop的文件夹:C:\ Windows\System32\config\systemprofile和C:\ Windows\SysWOW64\config\systemprofile

之后,将这两个文件夹的完全权限授予用户组IIS:IIS_IUSRS

这将导致此用户使用"桌面"工作,从而实现IIS Word使用库.

我粗暴地帮助某人.