Andy Wiggins wrote...
Here's some code that might help. It tests whether a file is open: if it is
it activates it, otherwise it opens the file.
If IsWindowOpen(lStr_FileName) Then
Windows(lStr_FileName).Activate
Else
Workbooks.Open FileName:=lStr_Path & lStr_FileName
End If
''
***************************************************************************
'' Purpose : Test if a specific file is open
'' Written : 29-Jun-2001 by Andy Wiggins
'' Syntax : IsWindowOpen("FileName.Xls")
'' Returns : "True" or "False"
''
Function IsWindowOpen(pstr_WindowName As String) As Boolean
Dim w As Window
For Each w In Windows
If w.Caption = pstr_WindowName Then
....
Why iterate through *Windows* rather than through *Workbooks*? There
are potential problems with your approach. First, if some files have
multiple windows, then those windows' .Caption properties would include
':#' tags following the filename, so your function is guaranteed always
to return FALSE when the file in question happens to have multiple
windows unless the user is clever enough to append ':1' to the filename
argument. Also, you'd iterate through all windows for workbooks with
multiple windows, which is unnecessary and a slight performance drag.
Second, the .Caption property of windows *CAN* *BE* *CHANGED* from the
workbook's filename, so there's no guarantee your approach would work
even if the file in question were open with only a single window.
If you want to iterate, *FAR* *BETTER* to iterate over the Workbooks
collection and check the .Name property. However, better still not to
iterate at all. See Bob Phillips's response.