2017-06-20 64 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

相關問題