Bonjour,
Je voudrais savoir s'il existe un moyen en VBA Excel de tester si un fichier est ouvert par un autre utilisateur.
Merci d'avance
Bonjour,
Je voudrais savoir s'il existe un moyen en VBA Excel de tester si un fichier est ouvert par un autre utilisateur.
Merci d'avance
Bonjour mach1974
Merci pour ta réponse.
J'avais espéré éviter de passer par un On Error mais s'il n'est pas possible de faire autrement, il faudra bien s'y résoudre.
Hello,
voici un code proposé dans le forum il y a quelques années par mfoxy, qui donne l'utilisateur ayant ouvert le fichier, si le fichier a été déjà ouvert :
Ami calmant, J.P
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Sub testFileInUse() MsgBox Excel_File_in_use_by("d:\Excel\MonFichierExcel.xlsm") End Sub Function Excel_File_in_use_by(FilePath As String) As String Dim strTempFile As String Dim iPos As Integer, iRetVal As Integer Dim objFSO As Object, objWMIService As Object, objFileSecuritySettings As Object, objSD As Object iPos = InStrRev(FilePath, "\") strTempFile = Left(FilePath, iPos - 1) & "\~$" & Mid(FilePath, iPos + 1) Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(strTempFile) Then Set objWMIService = GetObject("winmgmts:") Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strTempFile & "'") iRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD) If iRetVal = 0 Then Excel_File_in_use_by = objSD.Owner.name Else Excel_File_in_use_by = "unknown" End If Set objWMIService = Nothing Set objFileSecuritySettings = Nothing Set objSD = Nothing Else Excel_File_in_use_by = vbNullString End If Set objFSO = Nothing End Function
Partager