Option Explicit
Sub Tst()
Dim sChemin As String
sChemin = ThisWorkbook.Path
With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = sChemin & "\"
.Title = "Sélectionner le Fichier"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
.ButtonName = "Sélection Fichier"
.Show
If .SelectedItems.Count > 0 Then
DoEvents
TestIsFileOpen .SelectedItems(1)
End If
End With
End Sub
Private Sub TestIsFileOpen(sFichier As String)
If IsFileOpen(sFichier) Then
MsgBox "File already in use!"
Else
MsgBox "File not in use!"
'Workbooks.Open sFichier
End If
End Sub
Private Function IsFileOpen(filename As String) As Boolean
Dim filenum As Integer, errnum As Integer
On Error Resume Next
filenum = FreeFile()
Open filename For Input Lock Read As #filenum
Close filenum
errnum = Err
On Error GoTo 0
Select Case errnum
Case 0
IsFileOpen = False
Case 70
IsFileOpen = True
Case Else
Error errnum
End Select
End Function
' Brutal .....
Private Function IsFileOpenLight(filename As String) As Boolean
Dim fichier As Integer
On Error Resume Next
fichier = FreeFile()
Open filename For Input Access Read Lock Read Write As fichier
If Err.Number = 0 Then
IsFileOpenLight = False
Close fichier
Else
IsFileOpenLight = True
End If
End Function