Search code examples
c#ms-wordvstoadd-indocx

get the path and name of the docx that is currently running c#


I'm coding an add-in for word , On the following code I take all the words that is in the file and put them in a dictionary.

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

And I want the file name of the docx that is currently running, instead of writing "monfichiertxt.docx".

Can someone help me please Thank you.


Solution

  • You can use the Name property or the FullName property like this

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

    Name will give you "MyDocument.docx" and FullName will give you "...\MyFolder\MyDocument.docx"