C# Get list of opened Word documents

会有一股神秘感。 提交于 2021-02-07 03:05:26

问题


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
}

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);

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?


回答1:


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");

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();

so the files cannot be opened and listed by the two instances of 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);


来源:https://stackoverflow.com/questions/18892018/c-sharp-get-list-of-opened-word-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!