C# 获取打开的 Word 文档列表 [英] C# Get list of opened Word documents

查看:61
本文介绍了C# 获取打开的 Word 文档列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用此代码获取 MS Word 打开的文档列表:

Currently, I'm using this code to get the list of MS Word opened documents:

List<string> doc_list = new List<string>();
try
{
    Microsoft.Office.Interop.Word.Application WordObj;
    WordObj = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
    for (int i = 0; i < WordObj.Windows.Count; i++)
    {
        object idx = i + 1;
        Window WinObj = WordObj.Windows.get_Item(ref idx);
        doc_list.Add(WinObj.Document.FullName);
    }
}
catch
{
    // No documents opened
}

如果文档是直接"打开的,即通过双击它们,它可以正常工作.但是,我注意到如果我直接从 C# 代码打开一个 MS Word 文档,例如:

And it correctly works if the documents have been opened "directly", i.e. by double clicking on them. However, I noticed that if I open a MS Word document directly from C# code, like:

Microsoft.Office.Interop.Word.Application word_app = new Microsoft.Office.Interop.Word.Application();

object inputFile = selected_doc;    // "selected_doc" contains the document name
object confirmConversions = false;
object readOnly = false;
object visible = true;
object missing = Type.Missing;

Document doc = word_app.Documents.Open(
    ref inputFile, ref confirmConversions, ref readOnly, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref visible,
    ref missing, ref missing, ref missing, ref missing);

以这种方式打开的任何文档都没有被初始代码找到,但我需要检测它.为什么找不到?如何修改代码以便找到从 C# 打开的文档?

any document opened in this way is not found by the intial code, but I need to detect it. Why is it not found? How can I modify the code so that the document opened from C# will be found?

推荐答案

在第一个代码中,您获得打开的 Word 实例中打开的文档列表

In the first code, you get the list of opened documents in an opened instance of Word

Microsoft.Office.Interop.Word.Application WordObj;
WordObj = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

在第二部分代码中,您使用 New Word 实例创建一个新文档

and in the second part of code, you create a new document with a New instance of Word

Microsoft.Office.Interop.Word.Application word_app = new Microsoft.Office.Interop.Word.Application();

所以文件不能被两个 Word 实例打开和列出.

so the files cannot be opened and listed by the two instances of Word.

如果您希望第一种方法能够获取文件,您可以尝试从 Word 的当前实例打开文件:

If you want the first method be able to get the file, you may try to open the file from the current instance of Word:

Microsoft.Office.Interop.Word.Application word_app;
word_app = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")

object inputFile = selected_doc;    // "selected_doc" contains the document name
object confirmConversions = false;
object readOnly = false;
object visible = true;
object missing = Type.Missing;

Document doc = word_app.Documents.Open(
    ref inputFile, ref confirmConversions, ref readOnly, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref visible,
    ref missing, ref missing, ref missing, ref missing);

这篇关于C# 获取打开的 Word 文档列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆