System.Runtime.InteropServices.COMException: Word 在试图打开文件时遇到错误

clbynicai 2013-12-17 11:55:59
我在调试网站的时候出现: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");
}
}这个代码是网上找的,求各位帮帮忙看看!谢谢啊!
...全文
506 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyun0112 2013-12-17
  • 打赏
  • 举报
回复
看看fileName 对应的文件存在吗

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
clbynicai 2013-12-17
  • 打赏
  • 举报
回复
在转换为.HTML的时候又出现了错误,在帮我看看是不是上面的代码哪里有问题,谢谢!
Im_Sorry 2013-12-17
  • 打赏
  • 举报
回复
引用 1 楼 feiyun0112 的回复:
看看fileName 对应的文件存在吗 ***************************************************************************** 签名档: http://feiyun0112.cnblogs.com/
+1

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧