[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:
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. :wave:
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?
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:
Private Function FunctionSearchForThisWindow(ByVal searchWildCard As String, ByVal location As String) As String
Dim processes() As Process
Dim instance As Process
Dim process As New Process
processes = process.GetProcesses
'get all the process and store them in process variable
Dim currentWindowRead As String
Dim sizeOfSearch As Integer
sizeOfSearch = searchWildCard.Length
If (location = "beg") Then 'if the location to search is
'the beggining of the string
For Each instance In processes ' loop through each process
currentWindowRead = instance.MainWindowTitle
' store the current window in the loop
'in the currentWindowRead variable
If (currentWindowRead.Length > sizeOfSearch) Then
'some windows do not have a title, so I
'had to do this check above
currentWindowRead = currentWindowRead.Substring(0, sizeOfSearch)
If (currentWindowRead = searchWildCard) Then
Return instance.MainWindowTitle
End If
End If
Next
End If
Return "" 'return an empty string if nothing found
End Function
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
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.
Re: [RESOLVED] FindWindow wildcards?
Thank you sir. Unfortunately, no such luck in my search of the VB6 code bank :(
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
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.
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.
Re: [RESOLVED] FindWindow wildcards?
Quote:
Originally Posted by
si_the_geek
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!