VBA: accessing the applications name on the taskbar

elliotgr

Registered User.
Local time
Tomorrow, 01:19
Joined
Dec 30, 2010
Messages
67
Hi
Despite trawling the web and this site, I can't find a simple way to return the name of the programs on the taskbar. :banghead:
The reason is I can have many instances of an application running such as Excel, opened from different sources. I need to determine if a specific file is open. Instead of going via Taskmanager and the processes, is there an easier way to 'read' the captions of the items on the taskbar?
 
Can be any file.
I run an application that manipulates excel, word and text files. However I want to ensure that if a file with the same name is open on the PC, I can close them before opening the new ones. I remember years ago doing this with a few lines of code, but can't find it now.
 
Why are you needing to perform this check by the way?

"A file" being word, excel or txt files exclusively?
 
Correct, they are files. I am NOT looking for the application (Excel) but the file (MyExcelFile.xls) for example.
I run VBA code to manipulate files in Memory without opening them.
However if they are open on the PC (which can be from various file locations but could have the same file name) I need to close them first.
Instead of having to check the various filepaths and names, it would surely be easier to check the task bar to see what is open?
 
Yep I understand that. Your best bet is to use WMI to check what processes are running and for search part of the Task name for the file name. It's like using Task Manager to view all running Applications but through code.

Here's a site for this:

http://www.computerperformance.co.uk/vbscript/wmi_process.htm

If you're finding it difficult let us know.
 
Hi
Tried WMI but could not find the file name, I can pull back every thing else.
Do you know what the WMI property is?
I looked at the properties on the Microsoft site, and it doesn't seem to have one to return a filename or even a way of determining the filename.
 
There must be a Task Name or Taskbar Title in one of the fields in SELECT *? I don't know what the name is but if you look through all the fields you should find it. It's called Window Title in command prompt. Let's see your code.

If that doesn't work, then you will need to pipe the list of tasks to a file from command prompt using the Tasklist command, then open and read the file.
 
Here is the code to list the processes in Windows Task Manager.
However I want to list the Applications in Windows Task Manager.


Code:
Public Sub showProcesses()
Dim W As Object
Dim ProcessQuery As String
Dim processes As Object
Dim process As Object
Set W = GetObject("winmgmts:")
ProcessQuery = "SELECT * FROM win32_process"
Set processes = W.execquery(ProcessQuery)
For Each process In processes
MsgBox process.Name 
MsgBox process.Description
MsgBox process.CreationClassName
MsgBox process.WindowsVersion
Next
Set W = Nothing
Set processes = Nothing
Set process = Nothing
End Sub
 
Here is the code to list the processes in Windows Task Manager.
However I want to list the Applications in Windows Task Manager.


Code:
Public Sub showProcesses()
Dim W As Object
Dim ProcessQuery As String
Dim processes As Object
Dim process As Object
Set W = GetObject("winmgmts:")
ProcessQuery = "SELECT * FROM win32_process"
Set processes = W.execquery(ProcessQuery)
For Each process In processes
MsgBox process.Name 
MsgBox process.Description
MsgBox process.CreationClassName
MsgBox process.WindowsVersion
Next
Set W = Nothing
Set processes = Nothing
Set process = Nothing
End Sub
It looks like the Task Name is not present in the properties of win32_process:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx

I thought the Caption property was it but it wasn't.

Tasklist in cmd and piping out to a text file would be your next option then.
 
What do you mean by not what you wanted? It will get you the Task Name, e.g. "elliotgr.doc - Microsoft Word", you can pipe the result to a text file, then read each line frome the text file for "elliot.doc". Just another way of doing it.
 
Apologies, I was hoping to do it via WMI, but the cmd will work just fine. Am just frustrated because MS did not add a caption or applications property to WMI as I use it elsewhere, and like to keep the code clean.
Once again thanks.
 
It has a Caption propert but the property is useless for your purpose. It is a shame.
 

Users who are viewing this thread

Back
Top Bottom