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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| Sub TestLocalFullName()
Dim Z As String, X As String, Y As String
Z = ActiveWorkbook.FullName
X = LocalFullName$(Z)
Y = Dir(LocalFullName(ActiveWorkbook.FullName))
MsgBox Replace(X, Y, "")
End Sub
Public Function LocalFullName$(ByVal fullPath$)
'Trouve le chemin local pour une URL de fichier OneDrive, en utilisant les variables denvironnement de OneDrive
'Référence https://stackoverflow.com/questions/33734706/excels-fullname-property-with-onedrive
'Auteurs*: Philip Swannell 2019-01-14, MatChrupczalski 2019-05-19, Horoman 2020-03-29, P.G.Schild 2020-04-02
Dim ii&
Dim iPos&
Dim oneDrivePath$
Dim endFilePath$
If Left(fullPath, 8) = "https://" Then 'Possiblement une URL OneDrive
If InStr(1, fullPath, "my.sharepoint.com") <> 0 Then 'Commercial OneDrive
'Pour OneDrive commercial, le chemin daccès ressemble à «*https://companyName-my.sharepoint.com/personal/userName_domain_com/Documents*» et à «*file.FullName*».
'Trouver "/Documents" dans une chaîne et tout remplacer avant la fin par un chemin local OneDrive
iPos = InStr(1, fullPath, "/Documents") + Len("/Documents") 'trouver la position "/Documents" dans lURL du fichier
endFilePath = Mid(fullPath, iPos) 'Obtenez le chemin du fichier final sans pointeur dans OneDrive. Incluez le "/"
Else 'Personal OneDrive
'Pour OneDrive personnel, le chemin ressemble à "https://d.docs.live.net/d7bbaa########1/" et fichier.FullName
'Nous pouvons obtenir un chemin de fichier local en remplaçant "https.." jusquà la 4ème barre oblique, avec le chemin local OneDrive obtenu à partir du registre
iPos = 8 'Dernière barre oblique dans https://
For ii = 1 To 2
iPos = InStr(iPos + 1, fullPath, "/") 'trouver 4ème barre oblique
Next ii
endFilePath = Mid(fullPath, iPos) 'Obtenez le chemin du fichier final sans la racine OneDrive. Inclure le "/"
End If
endFilePath = Replace(endFilePath, "/", Application.PathSeparator) 'Remplacer les barres obliques vers lavant par des barres obliques vers larrière (type dURL vers type Windows)
For ii = 1 To 3 'Boucle pour voir si le LocalWorkbookName provisoire est le nom dun fichier qui existe réellement, le cas échéant renvoyer le nom
oneDrivePath = Environ(Choose(ii, "OneDriveCommercial", "OneDriveConsumer", "OneDrive")) 'Vérifiez les chemins locaux possibles. "OneDrive" devrait être le dernier
If 0 < Len(oneDrivePath) Then
LocalFullName = oneDrivePath & endFilePath
Exit Function 'Succès (c.-à-d. trouvé le bon paramètre Environ)
End If
Next ii
'Peut-être générer une erreur ici lorsque la tentative de conversion vers un nom de fichier local échoue - par ex. pour les fichiers "partagés avec moi"
LocalFullName = vbNullString
Else
LocalFullName = fullPath
End If
End Function |
Partager