1

I'm checking word process instances, I have open more then one instances of word instance but it returns only the first instance of word and return process.Count 1 , When I use notepad as process then it works but with winword it didn't works.

For Each a In p
    If Not pName.Equals("") And I <= p.Count Then
        Console.WriteLine(a)
        Console.WriteLine(p.Count)
        pName = p(I).MainWindowTitle.ToString
        File.WriteLine("Word Process Name : {0} is started on time {1}", pName, p(I).StartTime)
        fullPath = Path.GetFullPath(pName)
        File.WriteLine("Path Of the file is  : {0}", fullPath(0))
    End If
Next
Salahudin Malik
  • 398
  • 4
  • 17
  • The code you have posted seems to be listing files on disk. You want to see the currently running word processes? Have a look at this https://stackoverflow.com/questions/11055147/how-do-i-get-list-of-process-names-running-in-vb-net – pmcilreavy Nov 06 '17 at 07:59
  • 1
    FYI windows and processes are not the same thing. Word could (and usually does) run multiple document windows in the same process. – Visual Vincent Nov 06 '17 at 08:12
  • 2
    You have not opened more than 1 instance of Word. Something you can see back in Task Manager, Processes tab. Word is a single instance program, it is too big. So starting it again simply gets the second instance to ask the first instance to open the document. And it quits. – Hans Passant Nov 06 '17 at 08:59
  • I have opened more than one instance of word – Salahudin Malik Nov 06 '17 at 09:08
  • if i get the instances of notepad is works but when i use winword it didn't works – Salahudin Malik Nov 06 '17 at 10:07
  • 1
    As Hans says, if you run Word twice, the second instance just passes information to the first instance telling it to open a new window and then quits. You'll see two windows but there'll only be one winword process. Notepad doesn't do that. The only exception would be if you're running two different *versions* of Word, but you've not suggested so far that that's what you're doing. – Damien_The_Unbeliever Nov 06 '17 at 10:39
  • `if i get the instances of notepad is works but when i use winword it didn't works` - Because Notepad is a multi-instance application while Word usually is not. You don't have multiple instances open, only one instance with _**multiple windows**_. Refer to Hans's comment. – Visual Vincent Nov 06 '17 at 10:40
  • So is there any solution to get the instances if word file – Salahudin Malik Nov 06 '17 at 11:31
  • You need to make a win32 API call. Look at this solution. http://www.vbforums.com/showthread.php?615831-RESOLVED-How-to-get-full-path-of-running-process&p=3807072#post3807072 – Chillzy Nov 06 '17 at 13:19
  • What's wrong with [the link I gave you four days ago?](https://stackoverflow.com/questions/47070401/im-trying-to-read-word-file-path-but-it-returns-me-the-wrong-path-in-vb-net-and#comment81096691_47070401) – Visual Vincent Nov 06 '17 at 15:54
  • @VisualVincent your solution is not working – Salahudin Malik Nov 07 '17 at 05:29

1 Answers1

2

I have solved This problem with Word.Application API you can add it form reference add.

Dim wordApp As Microsoft.Office.Interop.Word.Application
wordApp = Marshal.GetActiveObject("Word.Application")
For Each f In wordApp.Documents
          pName = Path.GetFileName(f.FullName).ToString()
           pPath = f.Path.ToString()
           File.WriteLine("Word Process Name : {0}  ", pName)
           File.WriteLine("File Path is: {0} ", pPath)
           File.WriteLine("File Starting Time is {0}", pTime)                                
Next
Salahudin Malik
  • 398
  • 4
  • 17