Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have following code to get title of current opened excel file this code working fine. I use timer to every 10 seconds if title change then add new title in list1.

So question is there any method or event to detect if title change then my code work otherwise it not work not check. timer check every 10 seconds my pc work slow if I run this code



VB
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
      List1.AddItem (sTitle)
    End If

    hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
  Wend

Private Sub Timer1_Timer()
 ListWins "*.xls*"
End Sub
Posted
Updated 15-Sep-13 17:51pm
v4
Comments
Ron Beyer 15-Sep-13 10:34am    
That's just a code dump, where is the question?
Menon Santosh 15-Sep-13 10:55am    
Question is missing, go to improve question option and edit your question

1 solution

Your search will not find anything since your class name is not valid. See the documentation for the FindWindow function[^] for the correct syntax.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900