Function ExtraireFinChemin2$(txt$, Optional chx As Boolean = True)
Dim regEx As Object, wf As WorksheetFunction, NbOccurrence As Byte
Set regEx = CreateObject("VBScript.RegExp")
Set wf = Application.WorksheetFunction
NbOccurrence = (Len(txt) - Len(Replace(txt, "\", "", , , 1))) / Len("\") 'nombre de fois qu'apparaît "\" dans la chaîne "txt"
With regEx
.Pattern = wf.Rept("(.*)\\", NbOccurrence - IIf(chx, 0, 1)) & "((.*)\.(.*))"
.Global = True
.MultiLine = True
.IgnoreCase = False
End With
If regEx.test(txt) Then
ExtraireFinChemin2 = regEx.Replace(txt, "$" & NbOccurrence + 1)
If NbOccurrence = 1 Then 'le fichier se trouve dans la racine (ex : "C:\La chèvre de M. Seguin.pdf")
regEx.Pattern = "(.*)\\(.*)"
ExtraireFinChemin2 = regEx.Replace(ExtraireFinChemin2, "$2")
End If
Else
ExtraireFinChemin2 = "Not matched"
End If
End Function