Results 1 to 10 of 10

Thread: [RESOLVED] FindWindow wildcards?

  1. #1

    Thread Starter
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    Resolved [RESOLVED] FindWindow wildcards?

    Let's say you wanted to get a handle to a window using findwindow and all you can use is the windows title.

    The classname will not work because their are windows with the same classname.

    Let's say the title of the window will always vary. Example....


    ExternalWindow Version 1
    ExternalWindow Version 2

    Is there a way to use findwindow with wildcards at the end.

    I tried......

    VB Code:
    1. Mainhwnd = FindWindow(vbNullString, "ExternalWindow Version")

    and it did not get a handle. It only gets a handle if I type in the full title name.

    Any help, tips, links would be great. Thanks again.
    Last edited by SpagettiProg; Jun 26th, 2006 at 11:33 AM.
    If you feel my post has helped, please rate it
    http://www.silentthread.com

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: FindWindow wildcards?

    Kind of confused, but cant you just use the classname and the windowname to find it instead of using one or the other?

  3. #3

    Thread Starter
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    Re: FindWindow wildcards?

    I figured it out. I wrote the following function which takes care of what I need. The function below works if you want to start searching from the beginning of the title. But you can probably customize it and add code so it can handle say an "end" parameter.

    You can basically call it like this...

    Dim fulltitle As String
    fulltitle = FunctionSearchForThisWindow("ExternalWindow Version", "beg")

    VB Code:
    1. Private Function FunctionSearchForThisWindow(ByVal searchWildCard As String, ByVal location As String) As String
    2.  
    3.  
    4.         Dim processes() As Process
    5.         Dim instance As Process
    6.         Dim process As New Process
    7.         processes = process.GetProcesses
    8.         'get all the process and store them in process variable
    9.         Dim currentWindowRead As String
    10.         Dim sizeOfSearch As Integer
    11.         sizeOfSearch = searchWildCard.Length
    12.         If (location = "beg") Then 'if the location to search is
    13.             'the beggining of the string
    14.             For Each instance In processes ' loop through each process
    15.                 currentWindowRead = instance.MainWindowTitle
    16.                 ' store the current window in the loop
    17.                 'in the currentWindowRead variable
    18.                 If (currentWindowRead.Length > sizeOfSearch) Then
    19.                     'some windows do not have a title, so I
    20.                     'had to do this check above
    21.                     currentWindowRead = currentWindowRead.Substring(0, sizeOfSearch)
    22.                     If (currentWindowRead = searchWildCard) Then
    23.                         Return instance.MainWindowTitle
    24.                     End If
    25.                 End If
    26.             Next
    27.         End If
    28.  
    29.         Return "" 'return an empty string if nothing found
    30.  
    31.     End Function
    If you feel my post has helped, please rate it
    http://www.silentthread.com

  4. #4
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: [RESOLVED] FindWindow wildcards?

    Hello,

    Is there a way to modify the code to work for VBA? "process" and "return instance" is not recognized by VBA.

    Thank you,

    - Juatair07

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] FindWindow wildcards?

    The objects involved (such as Process and .MainWindowTitle) are .Net specific, so the code is not appropriate for VBA etc.

    The functionality is available via various API's (such as EnumProcesses and GetWindowText), so you could use the code above as a guide for the code to write (putting appropriate API calls into relevant places)... but it is likely that VBA code already exists to do what you want, so searching for a VBA (or VB6) version is likely to work. I suspect our CodeBank forums have useful examples.

  6. #6
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: [RESOLVED] FindWindow wildcards?

    Thank you sir. Unfortunately, no such luck in my search of the VB6 code bank

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] FindWindow wildcards?

    There is a VB6 code example here that should work for you (just replace the MsgBox line with something to check the text, and do whatever you want if it is found):
    http://www.vbforums.com/showthread.p...=1#post1434919

  8. #8
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: [RESOLVED] FindWindow wildcards?

    I guess I'm not looking for the right solution. I was looking for a way to find an excel window that starts with "BlahBlahBlah - 1" or maybe "BlahBlahBlah - 2", etc. So the function would look for "BlahBlahBlah*"

    Similar to this, but I know wildcards won't work this way.

    Code:
    dbwnd = FindWindow(vbNullString, "BlahBlahBlah*")
    Essentially, I don't want to reference the process since I could have multiple EXCEL files open at one time and I don't want to effect all of them.

    Sorry for the confusion.
    Last edited by justair07; Jul 10th, 2018 at 11:40 AM.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] FindWindow wildcards?

    The code in link I provided could possibly be used for that (or easily extended by people who know how), but as the scope of your question is so different it is best to make a new thread in the Office Development forum.

  10. #10
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: [RESOLVED] FindWindow wildcards?

    Quote Originally Posted by si_the_geek View Post
    as the scope of your question is so different it is best to make a new thread in the Office Development forum.
    Understood. Thank you again!

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