System.Runtime.InteropServices.COMException: Word 在试图打开文件时遇到错误
我在调试网站的时候出现:System.Runtime.InteropServices.COMException: Word 在试图打开文件时遇到错误。
请尝试下列方法:
* 检查文档或驱动器的文件权限。
* 确保有足够的内存和磁盘空间。
* 用文本恢复转换器打开文件。
代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using USTC;
using Word=Microsoft.Office.Interop.Word;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
public partial class Inform_QE_Notice : System.Web.UI.Page
{
public string countyName = string.Empty; //乡镇名称
public string documentFullName = string.Empty; //预案全称(包括后缀名)
public string documentName = string.Empty; //预案名称
protected void Page_Load(object sender, EventArgs e)
{
//GetGoodsInfo();
if (!IsPostBack)
{
try
{
//根据传递过来的乡镇名称获取到文件名称
countyName = Server.UrlDecode(Request.QueryString["id"].ToString().Trim());
DM dm = new DM();
string strSQL = "select documentName from QE_Inform where informType='" + countyName + "'";
documentFullName = dm.getsql(strSQL).Tables[0].Rows[0]["documentName"].ToString().Trim();
documentName = documentFullName.Substring(0, documentFullName.LastIndexOf('.'));
}
catch (Exception)
{
documentFullName = "";
}
}
// 在此处放置用户代码以初始化页面
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents; // 打开文件
Type docsType = docs.GetType();
object fileName = Server.MapPath("~/Upload/") + documentFullName;
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
// 转换格式,另存为
Type docType = doc.GetType();
object saveFileName = Server.MapPath("~/Upload/") + documentName + ".html";
// ClientScript.RegisterClientScriptBlock(GetType(),"message",
//"<mce:script type="text/javascript"><!-- alert('"+saveFileName.ToString()+"'); --></mce:script>");
//保存HTML
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatHTML });
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//跳转显示预案信息
Response.Redirect("~/Upload/" + documentName + ".html");
}
}这个代码是网上找的,求各位帮帮忙看看!谢谢啊!