0

Is there a way to find the currently open file from an Excel or Word process etc.? I want to get the list of all running processes in Windows and which files they currently have open.

2

2 Answers 2

3

How about a list of running processes using VBA

Function getProcessInfo()
''On Error Resume Next
Dim objProcess, process, strNameOfUser
ComputerName = "."
Set objProcess = GetObject("winmgmts:{impersonationLevel=impersonate}\\" _
      & ComputerName & "\root\cimv2").ExecQuery("Select * From Win32_Process")
For Each process In objProcess
    If process.Name <> "System Idle Process" And process.Name <> "System" Then
        ''Debug.Print process.Name
        Debug.Print process.Name & "," & process.executablepath _
            & "," & process.Priority & "," & process.sessionid _
            & "," & strNameOfUser & "," & process.handlecount _
            & "," & process.ThreadCount
    End If
Next

Set objProcess = Nothing
End Function

Modified from : http://www.windowsadminscripts.com/coding/networking/processes/

Perhaps a list of open windows might be more useful:

Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Sub ListWins(Optional Title = "*", Optional Class = "*")
    Dim hWndThis As Long
    hWndThis = FindWindow(vbNullString, vbNullString)
    While hWndThis
        Dim sTitle As String, sClass As String
        sTitle = Space$(255)
        sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
        sClass = Space$(255)
        sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
        If sTitle Like Title And sClass Like Class Then
            Debug.Print sTitle, sClass
        End If
        hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
    Wend
End Sub

Use it like so:

ListWins "*.doc*"

This will list all Word windows with a title containing .doc

5
  • remou thank u very much for your code this code is show what process in runing with application for example wmplayer.exe proccess show the resulte c:\programfile\windows media player etc but not shown what file is runing in windows media player .. i need current runing file not current runing program Apr 19, 2012 at 12:21
  • You might like to read social.technet.microsoft.com/Forums/en/ITCG/thread/…
    – Fionnuala
    Apr 19, 2012 at 12:49
  • remou nothing this post.. handle.exe is good tool but its working on command prompt Apr 19, 2012 at 13:01
  • I have added some more notes.
    – Fionnuala
    Apr 19, 2012 at 13:07
  • thank u very much remou once again your code is working but still little bit problem your last code show the title of current file it *.doc working but if i use this code windows media player to detect current runing video file it shows nothing please check this link i want to add same functionality in vb6 nirsoft.net/utils/opened_files_view.html Apr 19, 2012 at 13:54
1

Is there a reason, why you use vb6?

Edit: I don't know, if that will help you, but here is a link, with some examples how to get a process list in VB6: http://wiki.robotz.com/index.php/Process_List_and_Locate_VB6

5
  • bcz i already done this in vb.net but every time when i install windows i need to installed .net framework if i done this in vb6...no need to installed anything Apr 19, 2012 at 11:13
  • Hwo do you do it in .NET? The same concepts/Win32 APIs will work in other languages too. Also, do you need your own app? Process Explorer does it already.
    – Deanna
    Apr 19, 2012 at 11:20
  • i need same funcationality like handle.exe in vb6 Apr 19, 2012 at 11:28
  • for example a windows media player runing in proccess a track avseq1.dat is runing i want to get runing track name in my vb6 application Apr 19, 2012 at 11:32
  • That will require custom support for every application to get "current documents" rather than jsut every file they have open (Some will open and close the file itself)
    – Deanna
    Apr 19, 2012 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.