阅读 Word 文档应用程序在 100 后崩溃+文件

发布于 2024-11-29 09:33:24 字数 1427 浏览 0 评论 0原文

我借用并修改了一个程序,如下所示。基本上问题是,经过一定数量的文件后,程序将崩溃。我追踪到了这个过程。不可避免的是,文档本身被证明是没有问题的,因此导致问题的原因是重复打开和关闭 Word 文档。而且它是随机的,有时会处理 100 个文件,有时会处理 800 个。有人有什么想法吗?当我在调试器中运行它时,我没有看到生成任何错误。该程序只是停止处理文件并变得无响应。我是否缺少一些垃圾收集?如何判断是否存在内存泄漏?

 private string readFileContent(string path)
 {
    string docstring = "";

    Word.ApplicationClass wordApp = new Word.ApplicationClass();
    object file = path;
    object nullobj = System.Reflection.Missing.Value;
    Word.Document doc = wordApp.Documents.Open(
      ref file, ref nullobj, ref nullobj,
      ref nullobj, ref nullobj, ref nullobj,
      ref nullobj, ref nullobj, ref nullobj,
      ref nullobj, ref nullobj, ref nullobj);

    try
    {
        SProc.Println("Created word doc instance");
        //doc.Clipboard.Clear();
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        SProc.Println("Copied text to clipboard");
        //System.Threading.Thread.Sleep(500);
        IDataObject data = Clipboard.GetDataObject();
        docstring = data.GetData(DataFormats.Text).ToString();
        txtFileContent.Text = docstring;
    }
    catch ( ConfigurationErrorsException e)
    {
            SProc.Println("Word Error:" +e.ToString());
    }
    finally
    {
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
    }
    return docstring;
 }

I have a procedure which I borrowed and modified shown below. Basically the problem is that after some amount of files, the program will crash. I traced it to this procedure. Inevitably the document itself proved to be fine, so it is something about the repeated opening and closing of word docs which is causing issues. And it's random, sometimes it will process 100 files, sometimes 800. Anyone have any thoughts? When I run it in the debugger I do not see any errors generated. The program just stops processing files and becomes unresponsive. Is there some garbage collection that I am missing? How can I tell if there is a memory leak?

 private string readFileContent(string path)
 {
    string docstring = "";

    Word.ApplicationClass wordApp = new Word.ApplicationClass();
    object file = path;
    object nullobj = System.Reflection.Missing.Value;
    Word.Document doc = wordApp.Documents.Open(
      ref file, ref nullobj, ref nullobj,
      ref nullobj, ref nullobj, ref nullobj,
      ref nullobj, ref nullobj, ref nullobj,
      ref nullobj, ref nullobj, ref nullobj);

    try
    {
        SProc.Println("Created word doc instance");
        //doc.Clipboard.Clear();
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        SProc.Println("Copied text to clipboard");
        //System.Threading.Thread.Sleep(500);
        IDataObject data = Clipboard.GetDataObject();
        docstring = data.GetData(DataFormats.Text).ToString();
        txtFileContent.Text = docstring;
    }
    catch ( ConfigurationErrorsException e)
    {
            SProc.Println("Word Error:" +e.ToString());
    }
    finally
    {
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
    }
    return docstring;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-12-06 09:33:24

另一个建议:

创建 Word.ApplicationClass 一次并在应用程序的生命周期中使用它,我怀疑您是否真的想要为 800 个文件启动它并关闭它 800 次。

Another suggestion:

Create the Word.ApplicationClass once and use it for the life of your application, I doubt you really want to start it and close it 800 times for 800 files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文