我有一个Web应用程序,它需要一些客户端信息并为每个客户端生成一封信。在我的测试环境中,一切正常,但在生产服务器上出现错误,说文件已损坏。我可以在服务器上打开.dotx文件,但无法通过代码打开。请帮忙。这里是我的代码:Com例外:Word无法阅读此文档。它可能会损坏
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,对象& DocumentType,对象&可见)+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(String eventArgument)+ 154 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+3691
不要在服务中自动化Word。 – SLaks 2010-10-05 16:50:44
其不在服务中。 – ecspot 2010-10-05 18:27:43