1

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.

YowE3K
  • 23,852
  • 7
  • 26
  • 40
titi2fois
  • 27
  • 7
  • Possible duplicate of [How to access Microsoft Word existing instance using late binding](https://stackoverflow.com/questions/2203968/how-to-access-microsoft-word-existing-instance-using-late-binding) – BugFinder Jun 20 '17 at 09:30

2 Answers2

1

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"

Gareth
  • 913
  • 6
  • 14
0

This question has been handled before and having never tried it, 1 google, and 2 lines of code, I get a simple answer. Although as an addin, I'd not have expected you to need to do this to get the word instance you're already part of..

    Microsoft.Office.Interop.Word.Application word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
    label1.Text = word.ActiveDocument.Name;
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • You have to know that we don't have all the abilities of finding the answer of our questions/problems as easily as you. Thank You for your answer but that will not work for my case. – titi2fois Jun 23 '17 at 14:15