Count & Select Window

G

Guest

VBA rookie here.

Is it possible to get VBA to count the number of active applications in
Windows? I don’t mean counting the number of Excel workbooks opened within
the Excel application but the number of applications that are active within
Windows itself. Say if I have launched Word, Notepad, Powerpoint, Excel and
SAP application, the count should return 5.

If this is possible, then how can I also get it to Debug.Print each
applications’ name?

Currentlly, I’m using sendkeys "%{TAB}" to tab to-and-fro the next
application. This is very dangerous. With your answer, I hope to get VBA to
activate the intended application rather than using ALT+TAB.

Thanks in advance
 
G

Guest

I’m really a rookie with VBA & almost no knowledge of VB.

I’ve followed
http://vbnet.mvps.org/index.htmlcode/system/toolhelpprocesses.htm , copied
its codes into VBA & successfully run tested it where it return results on
the “processes†that are active in Windows. The results are exactly like what
I get to see in Task Manager’s Tab #2. But this is not exactly what I needed.

What I actually need is contained within Tab # 1 (tab named: Applications)
in Task Manager. For example, say if there are 10 tasks listed in tab #1 of
Task Manager, how can I get VBA to “Switch To†the task that I needed?

(a) How can I get VBA to list down each tasks in a spreadsheet or by
Debug.Print (the list of tasks’ in Task Manger Tab #1).
(b) After achieving the above, how can I get VBA to “Switch To†the task of
my choice? I need the above steps for assurance because my procedure will
later be using SendKeys to perform data entry on a NON-MS Office based
application.

I’m aware that [Application.SendKeys "%{TAB <repeatition #>}", True] can
help do the switching but it’s simply too dangerous because it does not
verify if it has activated the intended screen. It will be a disaster if my
data entry (via SendKeys) is consequently executed on the wrong screen.

Thanking you in advance for the big favor.
 
N

NickHK

Edmund,
If you know which the class name of the app and/or it's title you are trying
to "connect" to, then FindWindow API would be easier.
I assume all the running processes are of no concern to you.

<From 'KPD-Team 1998: URL: http://www.allapi.net/ :E-Mail:
(e-mail address removed)>
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal
nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) +
"Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(WinWnd, lpClassName, 256)
'Show the classname
MsgBox "Classname: " + Left$(lpClassName, RetVal)
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub
</From 'KPD-Team 1998: URL: http://www.allapi.net/ :E-Mail:
(e-mail address removed)>

NickHK

Edmund said:
I'm really a rookie with VBA & almost no knowledge of VB.

I've followed
http://vbnet.mvps.org/index.htmlcode/system/toolhelpprocesses.htm , copied
its codes into VBA & successfully run tested it where it return results on
the "processes" that are active in Windows. The results are exactly like what
I get to see in Task Manager's Tab #2. But this is not exactly what I needed.

What I actually need is contained within Tab # 1 (tab named: Applications)
in Task Manager. For example, say if there are 10 tasks listed in tab #1 of
Task Manager, how can I get VBA to "Switch To" the task that I needed?

(a) How can I get VBA to list down each tasks in a spreadsheet or by
Debug.Print (the list of tasks' in Task Manger Tab #1).
(b) After achieving the above, how can I get VBA to "Switch To" the task of
my choice? I need the above steps for assurance because my procedure will
later be using SendKeys to perform data entry on a NON-MS Office based
application.

I'm aware that [Application.SendKeys "%{TAB <repeatition #>}", True] can
help do the switching but it's simply too dangerous because it does not
verify if it has activated the intended screen. It will be a disaster if my
data entry (via SendKeys) is consequently executed on the wrong screen.

Thanking you in advance for the big favor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top