You can search the window names. The search is case sensitive so make
sure your search string matches the file name.
Sub test()
Dim FName As String
Dim hWndStart As Long
Dim WindowText As String
hWndStart = 0
level = 0
WindowText = "*Ranks*.csv"
FName = FindWindowLike(hWndStart, _
WindowText, level)
set bk = Workbooks(FName)
Set shRanks = bk.Sheets(1)
End Sub
function to get name
------------------------------------------------------
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long)
_
As Long
Public Const GWL_ID = (-12)
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
'FindWindowLike
' - Finds the window handles of the windows matching the specified
' parameters
'
'hwndArray()
' - An integer array used to return the window handles
'
'hWndStart
' - The handle of the window to search under.
' - The routine searches through all of this window's children and
their
' children recursively.
' - If hWndStart = 0 then the routine searches through all windows.
'
'WindowText
' - The pattern used with the Like operator to compare window's
text.
'
'ClassName
' - The pattern used with the Like operator to compare window's
class
' name.
'
'ID
' - A child ID number used to identify a window.
' - Can be a decimal number or a hex string.
' - Prefix hex strings with "&H" or an error will occur.
' - To ignore the ID pass the Visual Basic Null function.
'
'Returns
' - The number of windows that matched the parameters.
' - Also returns the window handles in hWndArray()
'
'----------------------------------------------------------------------
Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, ByVal level As Integer) As String
Dim r As String
' Hold the level of recursion:
' Hold the level of recursion:
'Hold the number of matching windows:
Dim sWindowText As String
Dim sClassname As String
Dim sID
'return nothing if not found
FindWindowLike = ""
' Initialize if necessary:
If level = 0 Then
hWndStart = GetDesktopWindow()
End If
' Increase recursion counter:
level = level + 1
' Get first child window:
hWnd = GetWindow(hWndStart, GW_CHILD)
Do Until hWnd = 0
DoEvents ' Not necessary
' Search children by recursion:
r = FindWindowLike(hWnd, WindowText, level)
If r <> "" Then
FindWindowLike = r
Exit Function
Else
' Get the window text and class name:
sWindowText = Space(255)
r = GetWindowText(hWnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
If InStr(sWindowText, "Excel") Then
a = 1
End If
' Check that window matches the search parameters:
If sWindowText Like WindowText Then
FindWindowLike = sWindowText
Debug.Print "Window Found: "
Debug.Print " Window Text : " & sWindowText
Debug.Print " Window Handle: " & CStr(hWnd)
Exit Function
Else
' Get next child window:
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
End If
End If
Loop
End Function
--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread:
http://www.thecodecage.com/forumz/showthread.php?t=170681
Microsoft Office Help