Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

vba - How to get the filename of the process that is currently runing in vb6

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 & "
ootcimv2").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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...