目前,我正在使用此代码获取已打开的MS Word文档列表:
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文档,例如:
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#打开的文档能够被找到?