Find out what windows are open

N

NateBuckley

Hello everyone.

I remember coming across some type of tutorial/code example that would run
in Excel (VBA) and show you all the active window process names or something
similar to use in WinAPI calls (for the window handler).

I'm just wondering if anyone knows of this, and can point me in the correct
direction as I'm having no luck in finding it now.
 
S

Steven

I think this is it. It came from a previous post but I cant remember when.

This Workbook:

Option Explicit
Dim cControl As CommandBarButton

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").Select
End Sub

Private Sub Workbook_Open()
'Take away the Menu item for now
'Workbook_AddinInstall
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Workbook_AddinUninstall
End Sub

Private Sub Workbook_AddinInstall()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("WB Navigator
..qx").Delete
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
With cControl
..Caption = "WB Navigator &q"
..Style = msoButtonCaption
..OnAction = "ShowWorkbooks"
End With

On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("WB Navigator
q").Delete
On Error GoTo 0
End Sub

------------------------------------------------------------
Module:

Sub ShowWorkbooks()

Application.ScreenUpdating = False

Dim wb As Workbook
With UserForm1

..ListBox1.Clear
..ListBox2.Clear

For Each wb In Workbooks
.ListBox1.AddItem wb.Name
Next

Call SortListBox(.ListBox1)

.ListBox1.ListIndex = 0
.ListBox1.SetFocus
'MsgBox .ListBox1.Value

vListPosition = 0
Do Until .ListBox1.Value = ActiveWorkbook.Name
vListPosition = vListPosition + 1
.ListBox1.ListIndex = vListPosition
Loop


End With
UserForm1.Show False

End Sub


Sub SortListBox1(oLb As MSForms.ListBox)
Dim vaItems As Variant
Dim i As Long, j As Long
Dim vTemp As Variant

'Put the items in a variant array
vaItems = oLb.List

'Steal code from John Walkenbach’s Excel Power Programming
'with VBA to sort the array
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
If vaItems(i, 0) > vaItems(j, 0) Then
vTemp = vaItems(i, 0)
vaItems(i, 0) = vaItems(j, 0)
vaItems(j, 0) = vTemp
End If
Next j
Next i

'Clear the listbox
oLb.Clear

'Add the sorted array back to the listbox
'For i = LBound(vaItems, 1) To UBound(vaItems, 1)
' oLb.AddItem vaItems(i, 0)
'Next i
'This next code replace the above code by a Microsoft Group comment
04/18/08
oLb.List = vaItems

End Sub

Sub SortListBox(oLb As MSForms.ListBox)
Dim i As Long, j As Long
Dim vTemp As Variant

'with VBA to sort the array
For i = 0 To oLb.ListCount - 2
For j = i + 1 To oLb.ListCount - 1
If CStr(oLb.List(i, 0)) > CStr(oLb.List(j, 0)) Then
vTemp = CStr(oLb.List(i, 0))
oLb.List(i, 0) = CStr(oLb.List(j, 0))
oLb.List(j, 0) = vTemp
End If
Next j
Next i
End Sub
 
B

Bob Bridges

I think this code answers a question you asked in a different thread, Steven.

By the way, I thought last night I had found what Nathan asked for, but it
turned out to be a list not of running apps but of objects registered on a PC
- the character strings that can be used in a CreateObject call. There were
thousands of them, but an extract looks like this:

..
..
..
Vsn-Ind: Excel.Chart[.8]
Descrip: Microsoft Office Excel Chart

Vsn-Ind: [Excel.Sheet.12]
Descrip: Microsoft Office Excel 2007 Workbook

Vsn-Ind: [Excel.SheetMacroEnabled.12]
Descrip: Microsoft Office Excel 2007 Workbook

Vsn-Ind: [Excel.SheetBinaryMacroEnabled.12]
Descrip: Microsoft Office Excel 2007 Binary Workbook

Vsn-Ind: [Word.Document.6]
Descrip: Microsoft Word 6.0 - 7.0 Document

Vsn-Ind: [Word.Picture.6]
Descrip: Microsoft Word 6.0 - 7.0 Picture

Vsn-Ind: Word.Document[.8]
Descrip: Microsoft Word Document

Vsn-Ind: Word.Picture[.8]
Descrip: Microsoft Word Picture

Vsn-Ind: Word.Basic[.9]
Descrip: Microsoft Word Basic

Vsn-Ind: Word.Application[.11]
Descrip: Microsoft Word Application

Vsn-Ind: MSProject.Docfile.4[]
Descrip: Microsoft Project Proj95 Serializer

Vsn-Ind: Publisher.Application[.11]
Descrip: Microsoft Publisher Application
..
..
..
 

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