/// <summary>
        /// 读取word的文本内容,返回读取到的文本字符串(第一种方法)
        /// </summary>
        /// <param name="path">文件的完整路径</param>
        /// <param name="i">要读取的文本长度</param>
        /// <returns>返回内容</returns>
        public static string ReadWordFirst(object path, int i)
        {
            //if (!CheckPath(path.ToString()))
            //{
            //    return "";
            //}
            try
            {
                //初始化程序
                wordapp = new Microsoft.Office.Interop.Word.ApplicationClass();
                //打开word文档
                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);                //读取文件的文本
                text = doc.Content.Text;
                if (text.Length > i)
                {
                    text = text.Substring(0, i);
                }
                return text;
            }
            catch (Exception ex)
            {
                OperationTXT.Error(ex.Message, "c:/log/日志/系统日志/出错的Word/", "ReadWordFirst方法异常", "", true);
                string ReadwordSencond=ReadWordSencond(path, i);
                return ReadwordSencond;
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close(ref saveOptionFirst, ref oMissing, ref oMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    doc = null;
                }
                if (wordapp != null)
                {
                    wordapp.Quit(ref saveOptionFirst, ref oMissing, ref oMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordapp);
                    wordapp = null;
                }
                System.GC.Collect();
            }
        }这个方法去读取word的内容,如果发生异常就记录日志并且调用string ReadwordSencond=ReadWordSencond(path, i).这个方法是另外一个读取word的方法,在第二个方法里如果发生异常则调用第三个读word的方法.一共有5个方法,依次类推.但是有时候明明没有异常  进入finally时 finally
            {
                if (doc != null)
                {
                    doc.Close(ref saveOptionFirst, ref oMissing, ref oMissing);                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    doc = null;
                }
                if (wordapp != null)
                {
                    wordapp.Quit(ref saveOptionFirst, ref oMissing, ref oMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordapp);
                    wordapp = null;
                }
                System.GC.Collect();
            }
执行关闭word的时候,它会弹出一个对话框.然后如果你不点击这个对话框的取消按钮.程序就会一直卡在这里.
我现在的需求是我循环去读取word,假如10000篇word,读到100篇突然弹出这样的对话框,然后卡在这里.
这就不对了.
我想有什么方法可以阻止对话框弹出.或者捕获到这个对话框,在用程序去把它关闭.等下贴上对话框的图片...

解决方案 »

  1.   

    上面和右下边的是弹出的对话框。
    左边的是winform程序。
    程序上的一些参数说明.字符长度: i代表我要截取word里多少个字符
    ReadWordFirst(object path, int i)
    更新数量:代表我有多少篇文档要读下面的就是显示的信息了,进入第几次循环,成功读取到了多少,正在读取哪一篇。
      

  2.   

    最笨的办法就是看下这个弹出框的句柄  api 检测是否存在 ,存在就干掉
      

  3.   

    句柄查看工具 Spy++  vs 自带的