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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| '--------------------------------------------------------------------------------------------------------------
' format_Date_inFileName
'--------------------------------------------------------------------------------------------------------------
' Objet : Remplace les formats date placés dans une chaîne de caractères par la date passée en paramètre au
' format demandé.
' Auteur : Julien Dufour
' Date : 15.10.2005
' Entrées : Chaîne de caractère à modifier
' Date (facultatif - par défaut : date du jour)
' Caractères de séparation (facultatif - par défaut : '#')
' Sorties : Chaîne de caractère modifiée
' Commentaires : Accepte les définitions de format en anglais et en français
'--------------------------------------------------------------------------------------------------------------
Public Function format_Date_inFileName(strInput As String, Optional dtmRef As Date, _
Optional strSeparation As String) As String
On Error GoTo Err_format_Date_inFileName
'**** DECLARATION DES VARIABLES
Dim strFormat As String ' Contient le format de date à utiliser
Dim intNb_car_separation As Integer ' Indique le nombre de caractère dans la chaîne indicatrice de séparation
Dim intPos_separation_deb As Integer ' Indique la position dans la chaîne du caractèrede séparation de début
Dim intPos_separation_fin As Integer ' Indique la position dans la chaîne du caractèrede séparation de fin
Dim strOutput As String ' chaîne résultat
'**** INITIALISATION DES VARIABLES
If Nz(dtmRef, Date) = "00:00:00" Then dtmRef = Date
If Nz(strSeparation, "#") = "" Then strSeparation = "#"
intNb_car_separation = Len(strSeparation)
intPos_separation_deb = 1
intPos_separation_fin = 0
strFormat = ""
strOutput = ""
'**** CODE DE LA PROCEDURE/FONCTION --------------------------------
'--- Recherche de la première occurence du caractère de séparation (début de la définition d'un format)
intPos_separation_deb = InStr(1, strInput, strSeparation, vbTextCompare)
'--- Déplacement dans la chaîne pour déterminer les zones à remplacer
While intPos_separation_deb <> 0
'--- Recherche de la première occurence du caractère de séparation (fin de la définition d'un format)
intPos_separation_fin = InStr(intPos_separation_deb + 1, strInput, strSeparation, vbTextCompare)
'--- extraction de la chaîne définissant le format
strFormat = Mid(strInput, intPos_separation_deb + intNb_car_separation, intPos_separation_fin - (intPos_separation_deb + intNb_car_separation))
'--- Traduction de la descriptione français du format
strFormat = Replace(strFormat, "A", "Y")
strFormat = Replace(strFormat, "J", "D")
'--- Mise à jour des chaîne en entrée et en sortie
strOutput = strOutput & Left(strInput, intPos_separation_deb - 1) & Format(dtmRef, strFormat)
strInput = Right(strInput, Len(strInput) - (intPos_separation_fin + intNb_car_separation) + 1)
'--- Recherche de la première occurence du caractère de séparation (début de la définition d'un format)
intPos_separation_deb = InStr(1, strInput, strSeparation, vbTextCompare)
Wend
If strInput <> "" Then strOutput = strOutput & strInput
Exit_format_Date_inFileName:
'**** LIBERATION DE LA MEMOIRE
format_Date_inFileName = strOutput
Exit Function
'**** GESTION DES ERREURS
Err_format_Date_inFileName:
MsgBox Error$
Resume Exit_format_Date_inFileName
End Function |
Partager