1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| Sub OuvertOuNon()
Dim NomFich As String, Chemin As String
Dim xlApp As Object
Dim xlCL1 As Object
Chemin = "D:\xls\"
NomFich = "LaClasseur.xls" 'sans le chemin
Set xlApp = CreateObject("Excel.Application")
'Si Excel est fermé, génère une erreur
Set xlApp = GetObject(, "Excel.Application")
On Error Resume Next
'si le fichier est fermé, génère une erreur
Set xlCL1 = xlApp.Workbooks(NomFich)
On Error GoTo 0
MsgBox "Le classeur " & NomFich & " est-il ouvert ? " & Not xlCL1 Is Nothing
'si on veut travailler sur le fichier en lecture seule
If Not xlCL1 Is Nothing Then
Set xlCL1 = xlApp.Workbooks.Open(FileName:=Chemin & NomFich, ReadOnly:=True)
MsgBox xlCL1.worksheets("Feuil1").Cells(1, 1) 'juste pour tester
'Else
'si on veut fermer l'instance d'Excel.
'ATTENTION, si le classeur est ouvert, ferme Excel de l'utilisateur
'xlApp.Quit
'Set xlApp = Nothing
'Set xlCL1 = Nothing
End If
End Sub |
Partager