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 64 65 66 67 68 69 70 71 72 73 74 75 76
| Sub Registro()
ActiveDocument.SaveAs FileName:=Format(Date, "dd") & "/" & Format(Date, "mm") & "/" & Format(Date, "yyyy") & "" & Format(Time, "hh") & "h/" & Format(Time, "nn") & ".doc"
End Sub
Sub Test()
aDateSauve = Date
aTimeSauve = Time
aSuffixeDeDate = Trim(Str(Year(aDateSauve))) + "-"
If Month(aDateSauve) < 10 Then
aSuffixeDeDate = aSuffixeDeDate + "0" + Trim(Month(aDateSauve)) + "-"
Else
aSuffixeDeDate = aSuffixeDeDate + Trim(Month(aDateSauve)) + "-"
End If
If Day(aDateSauve) < 10 Then
aSuffixeDeDate = aSuffixeDeDate + "0" + Trim(Day(aDateSauve)) + "-"
Else
aSuffixeDeDate = aSuffixeDeDate + Trim(Day(aDateSauve)) + "-"
End If
If Hour(aTimeSauve) < 10 Then
aSuffixeDeDate = aSuffixeDeDate + "0" + Trim(Hour(aTimeSauve)) + "-"
Else
aSuffixeDeDate = aSuffixeDeDate + Trim(Hour(aTimeSauve)) + "-"
End If
If Minute(aTimeSauve) < 10 Then
aSuffixeDeDate = aSuffixeDeDate + "0" + Trim(Minute(aTimeSauve))
Else
aSuffixeDeDate = aSuffixeDeDate + Trim(Minute(aTimeSauve))
End If
'// Cas du document jamais sauvegardé
If (ActiveDocument.Path = "") And (InStr(ActiveDocument.Name, "Document") = 1) And (InStr(ActiveDocument.Name, ".") = 0) Then
ActiveDocument.SaveAs2 FileName:="Doc" + aSuffixeDeDate + ".docx", FileFormat:=wdFormatXMLDocument
Exit Sub
End If
'// Cas du document sauvegardé mais qui ne finit pas une date et/ou une heure correctes
If (ActiveDocument.Path <> "") And (InStr(ActiveDocument.Name, ".") > 0) Then
aName = Mid$(ActiveDocument.Name, 1, InStr(ActiveDocument.Name, ".") - 1)
If Len(aName) < Len("2013-07-24-00-00") Then
MsgBox "Nom de fichier incorrect", vbOKOnly + vbCritical
Exit Sub
End If
aNameDeRecup = Mid$(aName, 1, Len(aName) - Len("2013-07-24-00-00"))
aDateATester = Mid$(aName, Len(aName) - Len("2013-07-24-00-00") + 1)
If (InStr(aDateATester, "-") = 0) Or (Val(aDateATester)) > 2099 Then
MsgBox "Nom de fichier incorrect", vbOKOnly + vbCritical
Exit Sub
End If
aDateATester = Mid$(aDateATester, InStr(aDateATester, "-") + 1)
If (Val(aDateATester)) > 12 Then
MsgBox "Nom de fichier incorrect", vbOKOnly + vbCritical
Exit Sub
End If
aDateATester = Mid$(aDateATester, InStr(aDateATester, "-") + 1)
'// Ici on ne teste pas la validité du jour du mois mais uniquement si la plage correspond à une valeur de n'importe quel mois
If (Val(aDateATester)) > 31 Then
MsgBox "Nom de fichier incorrect", vbOKOnly + vbCritical
Exit Sub
End If
aDateATester = Mid$(aDateATester, InStr(aDateATester, "-") + 1)
If (Val(aDateATester)) > 23 Then
MsgBox "Nom de fichier incorrect", vbOKOnly + vbCritical
Exit Sub
End If
aDateATester = Mid$(aDateATester, InStr(aDateATester, "-") + 1)
If (Val(aDateATester)) > 59 Then
MsgBox "Nom de fichier incorrect", vbOKOnly + vbCritical
Exit Sub
End If
'// Donc ici on a un fichier dont le nom est contenu dans aNameDeRecup suivi d'une chaine du type "2013-07-24-00-00" ==> donc on considere que c'est bon
ActiveDocument.SaveAs2 FileName:=ActiveDocument.Path + "\" + aNameDeRecup + aSuffixeDeDate + ".docx", FileFormat:=wdFormatXMLDocument
Exit Sub
End If
End Sub |
Partager