Results 1 to 10 of 10

Thread: Can VBS detect classname & title text ?

  1. #1
    vb.elmar
    Guest

    Can VBS detect classname & title text ?

    Hi

    I want to detect the Internet Explorer's title text by VBS.

    example:
    When page1 is loaded, VBS should recognize the title
    text and send another "Sendkey" sequence as when page2 is loaded in Internet Explorer.

    Can VBS read the different title text ?

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    You should be able to get the <title> tag via HTML DOM.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    You'll need to find all the IE instances or your program's instances with FindWindowEx or EnumWindows. Then you can use GetWindowText to find out what the text is for each of the above windows. You'll want to use SendMessage with WM_Keydown and WM_Keyup to do the key pressing.

  4. #4
    vb.elmar
    Guest
    I only can use FindWindowEx in Visual Basic, but not
    in VBS.

    So, when i want to read window titles, i think i have to use
    VB.

    VBS can not do that - am i right?

    (i only want to know if VBS can do that, or not)

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    One of the problems with IE is that you cannot use the traditional GetObject on it and technially, every window opened by the operating system is a version of IE, though this version does not use the Application object. Anyway, to get what you want, you have to enumerate the windows that are opened, and read their titles. The code snippet below will locate any window that has VB in the browser's title. Basically, what it does is trap an error (I believe 438) to see if the Shell object supports the properties that I am asking for.

    Code:
    Dim objShell, objShellWindows, objWindow, objIEApp
    Dim x , lng_Count
    
    x=0
    Set objShell = WScript.CreateObject("Shell.Application") 
    Set objShellWindows = objShell.Windows 
    lng_Count = objShellWindows.Count
    
    For x=0 to lng_Count
    	On error Resume Next
    	Set objWindow = objShellWindows.Item(x) 
    	'Every item here represents one instance of IE.  If you have 3 instances open then you can get anyone of them by specifying 0,1 0r 2. You can get the count from objShellWindows.Count   
    	Set objIEApp = objWindow.Application 'get the application object 
    
    	if Instr(objIEApp.document.title, "VB")<>0 then
    		if Err=0 then
    			msgbox objIEApp.document.title
    			Exit For
    		Else
    			Err.number=0
    		End if
    	End if
    Next
    
    msgbox "Finished"
    Hope this helps.

  6. #6
    vb.elmar
    Guest
    So, you have solved my problem.

    I can use VBScript to detect **any** main window title
    of an Internet Explorer application.

    The last question would be:
    Can i detect the window titles of other programs too (notepad, bestcrypt, paint shop pro .. ) (?)

  7. #7
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    Put this code in a .bas module:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Declare Function EnumWindows Lib "user32" _
    4.    (ByVal lpEnumFunc As Long, _
    5.     ByVal lParam As Long) As Long
    6. Public Declare Function GetClassName Lib "user32" _
    7.     Alias "GetClassNameA" _
    8.    (ByVal hwnd As Long, _
    9.     ByVal lpClassName As String, _
    10.     ByVal nMaxCount As Long) As Long
    11. Public Declare Function GetWindowText Lib "user32" _
    12.     Alias "GetWindowTextA" _
    13.    (ByVal hwnd As Long, _
    14.     ByVal lpString As String, _
    15.     ByVal cch As Long) As Long
    16. Public Declare Function IsWindowVisible Lib "user32" _
    17.    (ByVal hwnd As Long) As Long
    18. Private Declare Function IsWindowEnabled Lib "user32" _
    19.    (ByVal hwnd As Long) As Long
    20. Public Declare Function GetParent Lib "user32" _
    21.     (ByVal hwnd As Long) As Long
    22.  
    23. Public Const MAX_PATH = 260
    24.  
    25. Public Function EnumWindowProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    26.     Dim sTitle  As String
    27.     Dim sClass  As String
    28.    
    29.     'eliminate windows that are not top-level.
    30.     If GetParent(hwnd) = 0& And _
    31.         IsWindowVisible(hwnd) And _
    32.         IsWindowEnabled(hwnd) Then
    33.        
    34.         GetWindowInfo hwnd, sTitle, sClass
    35.        
    36.         'add the items to the list
    37.         Form1.List1.AddItem sTitle & " (" & sClass & ")"
    38.         Form1.List1.ItemData(Form1.List1.NewIndex) = hwnd
    39.     End If
    40.    
    41.                        
    42.     'To continue enumeration, return True
    43.     'To stop enumeration return False (0).
    44.     'When 1 is returned, enumeration continues
    45.     'until there are no more windows left.
    46.     EnumWindowProc = 1
    47. End Function
    48.  
    49. Public Sub GetWindowInfo(hwnd As Long, Optional sTitle As String, Optional sClass As String)
    50.    
    51.     'set up the strings to receive the class and window text
    52.     sTitle = Space$(MAX_PATH)
    53.     sClass = Space$(MAX_PATH)
    54.    
    55.     Call GetClassName(hwnd, sClass, MAX_PATH)
    56.     Call GetWindowText(hwnd, sTitle, MAX_PATH)
    57.    
    58.     'strip the trailing chr$(0)'s from the strings returned above
    59.    
    60.     sClass = TrimNull(sClass)
    61.     sTitle = TrimNull(sTitle)
    62. End Sub
    63.  
    64. Public Function TrimNull(s As String)
    65.     'remove string before the terminating null(s)
    66.     Dim pos As Long
    67.    
    68.     pos = InStr(s, Chr$(0))
    69.    
    70.     If pos Then
    71.          TrimNull = Left$(s, pos - 1)
    72.     Else
    73.          TrimNull = s
    74.     End If
    75. End Function
    76.  
    77. Public Sub InitEnumWindows()
    78.     EnumWindows AddressOf EnumWindowProc, &H0
    79. End Sub
    Put this code inside form1, along with a list1 and command1:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     List1.Clear
    5.     InitEnumWindows
    6. End Sub
    The title will appear first, the window's class is in parentheses.

  8. #8
    vb.elmar
    Guest
    i mean VBS (not VB)

  9. #9
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    VBS really isn't designed for this... what are you trying to do?

  10. #10
    vb.elmar
    Guest
    1

    i'm wondering about VBS that can send keystrokes to every
    app, like:

    set ws = CreateObject("Wscript.shell")
    ws.sendkeys("Hello!")

    ..but it doesn't recognize to which window it sends the keystrokes.

    =============================================
    2

    On the desktop, i created a F9-hotkey-link to a vbs file.
    -No matter which program runs, when i press F9, the vbs
    file starts.

    The vbs-code should detect, if Bestcrypt is running
    (and if possible weather Bestcrypt's window is the foregroundwindow)
    or iE is running (and if possible weather iE's window is the foregroundwindow).

    -When iE is running, it sends Loginname/Password to the iE application.

    -When it detects that Bestcrypt.exe is running, it should send
    1xTab and the passwordphrase to the Bestcrypt window.

    -The problem is that VBS does not recognice, if it sends the
    keystrokes to the Internet Explorer or to the Bestcrypt
    application.

    But i only have *one* F9 hotkey, and therefore the VBS-code
    *MUST* distinguish between these different apps.

    ============
    That's the prob. (i know that i can use VB for it, but that's not my question)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width