Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Common Dialog Box for Find Files

Status
Not open for further replies.

cwalshe

Programmer
May 28, 2001
84
0
0
IE
Is it possible to get the Find files dialog box to appear using the common dialog control. I need to have the ability to search for files contaiing specific text.

If this is not possible, then I am currently using an API call to show the box but I want a way to close it other then File, Exit or using the 'x'.

Basicaly when I close one of my own forms I would like to close this form also.

Any ideas please...
 
You can use this code to close any window you desire:

Start with adding the following API Declarations to the Declarations section of a module:

Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10

And here's the code. Just replace apptitle with the exact title of the application you want to close in your case "Microsoft Word":

Dim winHwnd As Long
Dim RetVal As Long

winHwnd = FindWindow(vbNullString, "apptitle")

If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox &quot;Error posting message.&quot;
End If
Else
MsgBox &quot;Application is not open.&quot;
End If

Hope this helps!
 
Could I have the code to get the Find files dialog box to appear using the common dialog control?

Thanks in advance.
 
Its not Microsoft Word that I am trying to close but instead the Find Files Dialog, ie Start, Search, Find Files.

What should I input into AppTitle?
 
You should type in the word Start, Search, or Find files in place of AppTitle just as it appears the the title bar.
 
Public Sub ShowFindDialog(Optional InitialDirectory As String)

ShellExecute 0, &quot;find&quot;, _
IIf(InitialDirectory = &quot;&quot;, &quot;&quot;, &quot;c:\&quot;), _
vbNullString, vbNullString, SW_SHOW

End Sub



Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias _
&quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, ByVal nShowCmd _
As Long) As Long

Const SW_SHOW = 5
 
Sorry about the Microsoft Word stuff. I copied and pasted the code from another one of my posts.
 
Is there a way to execute the find with out displaying the box? I want to search through word documents looking for transcriptionists name. Then I want to take these files and copy them to a different directory. Do you know if this is possible.
 
Is there a way to execute the find with out displaying the box? I want to search through word documents looking for transcriptionists name. Then I want to take these files and copy them to a different directory. Do you know if this is possible.
 
Worked great but there is a same problem. The title of the Find Dialog changes with its search request,

ie it starts with Find: All files and then can become anytning, eg Find: File containing acgb

How can you keep the app title up to date.
 
Scratch what I gave you before and use this info:

Put this in a module:

Declare Function FindWindow Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long


Declare Function sendmessagebystring Lib &quot;user32&quot; Alias &quot;SendMessageA&quot; (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long


Declare Function getwindow Lib &quot;user32&quot; Alias &quot;GetWindow&quot; (ByVal hWnd As Long, ByVal wCmd As Long) As Long


Declare Function GetWindowText Lib &quot;user32&quot; Alias &quot;GetWindowTextA&quot; (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Declare Function GetWindowTextLength Lib &quot;user32&quot; Alias &quot;GetWindowTextLengthA&quot; (ByVal hWnd As Long) As Long
Public Const WM_CLOSE = &H10
Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_MAX = 5
Public Const GW_OWNER = 4

Function FindWindowByTitle(Title As String)
Dim a, b, Caption
a = getwindow(Form1.hWnd, GW_OWNER)
Caption = GetCaption(a)


If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
FindWindowByTitle = b
Exit Function
End If
b = a


Do While b <> 0: DoEvents
b = getwindow(b, GW_HWNDNEXT)
Caption = GetCaption(b)


If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
FindWindowByTitle = b
Exit Do
Exit Function
End If
Loop

End Function


Function GetCaption(hWnd)
Dim hwndLength%, hwndTitle$, a%
hwndLength% = GetWindowTextLength(hWnd)
hwndTitle$ = String$(hwndLength%, 0)
a% = GetWindowText(hWnd, hwndTitle$, (hwndLength% + 1))
GetCaption = hwndTitle$

End Function

Sub KillWin(Title As String)
Dim a, hWnd
hWnd = FindWindowByTitle(Title)
a = sendmessagebystring(hWnd, WM_CLOSE, 0, 0)
End Sub


And here's the code:

KillWin (&quot;Find:&quot;)

This should do it!!!
 
Thx.

Seems to work well now.

Appreciate you sticking with it.

Cormac.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top