Private Sub Workbook_Open()
Application.WindowState = xlMaximized
'Place above line in each file that will be opened.
'Choices are: xlMaximized, xlNormal, xlMinimized
'Use xlMinimized to hide them
If IsWorkbookOpen("2.xls") Then
'MsgBox "File ""2.xls"" is open."
Else
Workbooks.Open ("C:\2.xls")
End If
End Sub
Function IsWorkbookOpen(AWorkbookPathName As String) As Boolean
Dim wkb As Workbook, users As Variant
'The presence of shared files makes for the possibility that another user
'may have the file open. To test for this, I have inserted additional code
'into Zathras' routine that makes use of the workbook.UserStatus array:
'One obvious drawback to this approach is that you must actually
'open the file to check the .UserStatus property.
'But it is more accurate overall.
'VBAjedi code...Tek-Tips
On Error Resume Next
IsWorkbookOpen = False
For Each wkb In Workbooks
If UCase(wkb.Path & "\" & wkb.Name) = UCase(AWorkbookPathName) Then
IsWorkbookOpen = True
End If
Next wkb
If IsWorkbookOpen = False Then ' See if other user has shared file open
Set wkb = Workbooks.Open(AWorkbookPathName)
If Not wkb Is Nothing Then
users = wkb.UserStatus ' returns an array of user names, access times, and exclusive/shared mode indicator
If UBound(users, 1) > 1 Then ' multiple users have file open
IsWorkbookOpen = True
End If
wkb.Close
End If
End If
End Function