首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#读取word报错.解决思路

2013-04-09 
c#读取word报错...System.Runtime.InteropServices.COMException (0x800A1066): 命令失败在 Microsoft.Off

c#读取word报错...
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);
             }
         } 
[解决办法]
你在网上找一下,好像有几个Open重载方法,试一下别的Open方法,我当时也是这个问题,换了个方法就好了

热点排行