c#读取word报错...

hsjyyzx 2013-04-02 08:48:39
System.Runtime.InteropServices.COMException (0x800A1066): 命令失败

Microsoft.Office.Interop.Word.Documents.Open(Object& FileName, Object& ConfirmConversions, Object& ReadOnly, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Format, Object& Encoding, Object& Visible, Object& OpenAndRepair, Object& DocumentDirection, Object& NoEncodingDialog, Object& XMLTransform)

是不是哪个参数不对啊。。

下面是我写的读取word的方法
这个方法将word的文档内容以字符串的方式返回.
path代表word文档在电脑上的路径,i表示我要读取多少个字符

public static string ReadWord(object path,int i)
{
object readOnly = true;
object missing = System.Reflection.Missing.Value;
//初始化程序
Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass();

try
{
//打开指定文件
Document doc = wordapp.Documents.Open(ref path,
ref missing, ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
//判断文件中是否存在表格

//读取文件的文本
string text = doc.Content.Text;
if (text.Length > i)
{
text = text.Substring(0,i);
}
//关闭文档
doc.Close(ref missing, ref missing, ref missing);
return text;

}
catch (Exception ex)
{
throw ex;
}
finally
{
//退出word程序
wordapp.Quit(ref missing, ref missing, ref missing);
}
}
...全文
241 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsjyyzx 2013-04-02
  • 打赏
  • 举报
回复
引用 楼主 hsjyyzx 的回复:
System.Runtime.InteropServices.COMException (0x800A1066): 命令失败 在 Microsoft.Office.Interop.Word.Documents.Open(Object& FileName, Object& ConfirmConversions, Object& ReadOnly……
没有提示有重载的方法啊。。。 Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass(); 是new这个吗? //打开指定文件 Document doc = wordapp.Documents.Open(path, false, true, false, missing, missing, true , missing, missing, missing, missing, false , true , missing, true , missing);
Kim_Du 2013-04-02
  • 打赏
  • 举报
回复
你在网上找一下,好像有几个Open重载方法,试一下别的Open方法,我当时也是这个问题,换了个方法就好了

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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