获取当前正在运行 C# 的 docX 的路径和名称



我正在为word编写一个加载项,在下面的代码中,我获取文件中的所有单词并将它们放入字典中。

`
   Dictionary<string, string> motRap = new Dictionary<string, string>();
   Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
   Document document = application.Documents.Open("monfichiertxt.docx");
        // Loop through all words in the document.
        int count = document.Words.Count;
        for (int i = 1; i <= count; i++)
        {
            // Write the word.
            string text = document.Words[i].Text;
            //motRapport.Add(text);
            motRap.Add(text, "blabla");
        }
        // Close word.
        application.Quit();

我想要当前正在运行的docx的文件名,而不是写"monfichiertxt.docx"。

有人可以帮我吗谢谢。

可以使用 Name 属性或 FullName 属性,如下所示

string name = document.Name;
string fullName = document.FullName;

名称会给你"MyDocument.docx",全名会给你"...\MyFolder\MyDocument.docx">

这个问题以前处理过,从来没有尝试过,1 谷歌和 2 行代码,我得到了一个简单的答案。虽然作为一个插件,我没想到你需要这样做来获得你已经参与的单词实例。

    Microsoft.Office.Interop.Word.Application word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
    label1.Text = word.ActiveDocument.Name;

最新更新