1

How to retrieve the actual path of the All Microsoft word files if it is saved in drive. For example I have multiple ms word processes running and these are saved somewhere in the drive. How can I retrieve full path? eg "C:\Users\abc\Desktop\testnotpad.docx" Using below code I can get the notepad process detail, but the same code with little changes "WINWORD.exe" is not working for ms word.

   string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", "notepad.exe");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
    ManagementObjectCollection retObjectCollection = searcher.Get();
    foreach (ManagementObject retObject in retObjectCollection)
    {
        string CommandLine = retObject["CommandLine"].ToString();
        string path = CommandLine.Substring(CommandLine.IndexOf(" ") + 1, CommandLine.Length - CommandLine.IndexOf(" ") - 1);
    }
chtenb
  • 14,924
  • 14
  • 78
  • 116
navi
  • 63
  • 2
  • 10
  • You can't do this that way. Instead, you need to use Word automation. – SLaks Feb 02 '16 at 16:41
  • @SLaks any example code? using this Application.ActiveWorkbook.FullName i can only get active window detail – navi Feb 02 '16 at 16:42
  • Activeworkbook is Excel.....try ActiveDocument.FullName – Sorceri Feb 02 '16 at 16:44
  • it will return me the Active Document detail not All documents detail.. I need All opened files information – navi Feb 02 '16 at 16:46
  • @navi: Did you look at the `Documents` collection? – SLaks Feb 02 '16 at 16:47
  • And BTW, your technique won't work with Notepad either. Just try File, Open. – SLaks Feb 02 '16 at 16:48
  • Exactly what is not working for ms word? Have you tried looking at what the "CommandLine" string is using the debugger? – PaulF Feb 02 '16 at 16:50
  • Nop, actualy i'm new in C# i dont have much idea – navi Feb 02 '16 at 16:52
  • use Application.Documents and loop through the collection to get the FullName for each opened document. – Sorceri Feb 02 '16 at 16:52
  • @SLaks: OP has already had this explained to him in his previous question on how to do this for notepad : http://stackoverflow.com/questions/34932301/how-to-get-notepad-file-saved-location – PaulF Feb 02 '16 at 16:52
  • @Slaks i'm doing a task for All kind of files so now i'm working on ms office files.. as i wrote i'm new in C# – navi Feb 02 '16 at 16:56
  • @PaulF: Yes; that answer is wrong. This isn't actually possible for arbitrary programs. – SLaks Feb 02 '16 at 17:26
  • @PaulF I've checked through debuger. CommandLine doesn't contain file path information – navi Feb 02 '16 at 19:17

0 Answers0