titi2fois
titi2fois

Reputation: 27

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.

Upvotes: 1

Views: 895

Answers (2)

Gareth
Gareth

Reputation: 911

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"

Upvotes: 1

BugFinder
BugFinder

Reputation: 17858

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;

Upvotes: 0

Related Questions