1

I'm trying to read word file path but it returns me the wrong path in vb.net and I'm using Path.getfullpath

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
  • It looks like your question is mostly code, please add some comments. – fmgonzalez Nov 02 '17 at 08:14
  • 1
    `Path.GetFullPath()` can't just magically lookup the path for whatever you pass to it. All it does is to ensure that you have no relative paths, i.e. `Hello.txt` would become `C:\Program Files (x86)\yourApp\Hello.txt` if your application's working directory is `C:\Program Files (x86)\yourApp`. – Visual Vincent Nov 02 '17 at 08:15
  • 1
    Possible duplicate of [C#: How to get the full path of running process?](https://stackoverflow.com/questions/5497064/c-how-to-get-the-full-path-of-running-process) – Visual Vincent Nov 02 '17 at 08:18
  • then how can I get out of the default path – Salahudin Malik Nov 02 '17 at 09:15
  • The link you have given above is get the path of executable file but I want the path of MainWindowsTitle – Salahudin Malik Nov 02 '17 at 09:23
  • ...which is why it is important to add comments to the question **explaining** specifically what you want, so that readers can understand what you're trying to achieve. – Visual Vincent Nov 02 '17 at 11:36
  • https://social.msdn.microsoft.com/Forums/office/en-US/fd0411cb-dba4-48a9-acf7-2575ade4e597/get-list-of-all-open-word-documents-in-all-word-instances – Visual Vincent Nov 02 '17 at 11:36

2 Answers2

1

You can also you Microsoft.Office.Interop.Word Library

Dim wordApp As Microsoft.Office.Interop.Word.Application
wordApp = Marshal.GetActiveObject("Word.Application")
FileTxt = My.Computer.FileSystem.OpenTextFileWriter("E:\txt.txt", True)
For Each f In wordApp.Documents
      pName = Path.GetFileName(f.FullName).ToString()
      pPath = f.Path.ToString()
      FileTxt.WriteLine("Word Process Name : {0}  ", pName)
      FileTxt.WriteLine("Path of File : {0} " , pPath)
Next
Salahudin
  • 96
  • 1
  • 3
0

when you use a for each loop avoid doing p(I) instead of "a". This will return all word process open

 Dim p() As Process = System.Diagnostics.Process.GetProcessesByName("winword")
        Dim List As New List(Of String)
        Dim cList As New List(Of Int32)
        Dim I As Int32 = 0
        If p.Count > 0 Then
            For Each a In p
                Dim fullpath As String = ""
                Console.WriteLine(a)
                Console.WriteLine(p.Count)
                Console.WriteLine("Word Process Name : {0} is started on time {1}", a.MainWindowTitle, a.StartTime.ToString)
                fullpath = Path.GetDirectoryName(a.MainModule.FileName)
                Console.WriteLine("Path Of the file is  : {0}", fullpath)
                cList.Add(I)
                I += 1
                List.Add(a.MainWindowTitle)
            Next
        Else
            'Word not open
        End If

you can use something like that to find the file in your system but this might take a while.

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
    My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, a.MainWindowTitle)

    msgbox(foundFile)
Next
Chillzy
  • 468
  • 3
  • 9