我有一个网络应用程序,它获取一些客户信息并为每个客户生成一封信。在我的测试环境中一切正常,但在生产服务器上我收到一条错误,指出文件已损坏。我可以在服务器上用 word 很好地打开 .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();
错误是:
Server Error in '/Tracker2' Application.
Word 无法读取此文档。它可能已腐败。 尝试以下一项或多项操作: * 打开并修复文件。 * 使用文本恢复转换器打开文件。 描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.Runtime.InteropServices.COMException:Word 无法读取此文档。它可能已腐败。 尝试以下一项或多项操作: * 打开并修复文件。 * 使用文本恢复转换器打开文件。
来源错误:
执行当前 Web 请求期间生成未处理的异常。有关异常来源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[COMException (0x800a141f):Word 无法读取此文档。它可能已腐败。 尝试以下一项或多项操作: * 打开并修复文件。 * 使用文本恢复转换器打开文件。] Microsoft.Office.Interop.Word.Documents.Add(对象与模板、对象与 NewTemplate、对象与文档类型、对象与可见)+0 Tracker.RunLetter2.Button1_Click(对象发送者,EventArgs e)位于 C:\Users\Ethan\Documents\Visual Studio 2010\Projects\EstateTracker\Tracker\RunLetter2.aspx.cs:52 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument)+154 System.Web.UI.Page.ProcessRequestMain( bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint)+3691
最佳答案
我知道是时候打开该项目了,但由于我在任何地方都没有找到答案,因此为需要它的人提供以下解决方案。
1 - 在IIS中,应用程序使用的应用程序池,将Identity属性更改为LocalSystem
2 - 在服务器上的以下目录中创建一个名为 Desktop 的文件夹: C:\Windows\System32\config\systemprofile 和 C:\Windows\SysWOW64\config\systemprofile
之后,将这两个文件夹的完全权限授予用户组IIS:IIS_IUSRS
这将导致这个用户拥有一个“桌面”来工作,从而实现IIS Word使用库。
我大概可以帮助别人。
关于asp.net - Com 异常 : Word was unable to read this document. 它可能已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3865854/