2017-06-20 88 views
0

我正在为单词编写一个加载项,在下面的代码中,我将文件中的所有单词放在字典中。获取当前正在运行的docx的路径和名称c#

` 
    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”。

有人可以帮我请 谢谢。

+0

可能的复制[如何使用后期绑定访问Microsoft Word中存在的实例]( https://stackoverflow.com/questions/2203968/how-to-access-mic rosoft-word-existing-instance-using-late-binding) – BugFinder

回答

0

可以使用Name财产或财产FullName这样

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

名称会给你“MyDocument.docx”和全名会给你“... \ MyFolder文件\ MyDocument.docx”

+0

谢谢,这正是我所寻找的 – titi2fois

0

此问题已经过处理,从未尝试过,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; 
+0

你必须知道,我们没有像你一样容易找到我们问题的答案。谢谢你的回答,但这对我的情况不起作用。 – titi2fois

相关问题