日期:2014-05-17  浏览次数:21017 次

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