Dim numero$ 'mémorisation
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
'interdit la modification du numéro de facture
Application.EnableEvents = False
With Sheets("Facture de service")
If Sh.Name = .Name Then _
If Not Intersect(Source, .[F4]) Is Nothing Then .[F4] = numero
End With
Application.EnableEvents = True
End Sub
Private Sub Workbook_Open()
If Not IsError([FichierCopié]) Then Exit Sub 'neutralisation
Dim chemin$, nomfich$, n As Variant
Sheets("Facture de service").Activate 'au cas où...
'---date du jour---
[F5] = Date
'---numéro de facture---
chemin = Me.Path & "\" 'chemin du dossier à adapter
nomfich = Dir(chemin & "Numero_Facture.xls*")
If nomfich <> "" Then
n = ExecuteExcel4Macro("'" & chemin & "[" & nomfich & "]Feuil1'!R1C1")
n = IIf(Left(n, 4) = CStr(Year(Date)), Val(Mid(n, 9)), 0)
End If
numero = "'" & Format(Date, "yyyymmdd") & Format(n + 1, "0000")
[F4] = numero
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
'Si l'on veut modifier le fichier modèle (vide) laisser le nom vide
Dim nom$, chemin$, wb As Workbook
With Sheets("Facture de service")
.Activate
If IsError([FichierCopié]) Then nom = Application.Proper(.[D10]) & "\"
'si l'on est sur le fichier service.xlsm la variable nom = ""
If nom = "\" Then
MsgBox "Le nom n'a pas été entré..."
.[D10].Select
Else
Cancel = True
chemin = Me.Path & "\" 'chemin du dossier
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
'---création/modification du fichier facture .pdf---
MkDir chemin & nom 'création du sous-dossier
.ExportAsFixedFormat xlTypePDF, chemin & nom & .[F4]
If Dir(chemin & nom & "*") = "" Then _
MsgBox "Caractère interdit dans le nom !", 48: Exit Sub
Application.EnableEvents = False
'---création/modification du fichier facture .xlsx---
Set wb = Workbooks.Add(xlWBATWorksheet)
.Copy After:=wb.Sheets(1)
wb.Sheets(1).Delete
wb.Sheets(1).Cells.Validation.Delete 'pour détruire les liaisons
wb.SaveAs chemin & nom & .[F4] 'enregistrement
wb.Close
'---création/modification du fichier service.xlsm---
Me.Names.Add "FichierCopié", True, Visible:=False 'nom défini masqué
Me.SaveAs chemin & nom & .[F4] & "_service", 52 'enregistrement
Application.EnableEvents = True
'---mémorisation du numéro de facture---
If nom <> "" Then
Workbooks("Numero_Facture").Close False 'si le fichier est ouvert
Set wb = Workbooks.Add(xlWBATWorksheet)
.[F4].Copy wb.Sheets(1).[A1]
wb.Sheets(1).Protect "TOTO" 'mot de passe à adapter
wb.SaveAs chemin & "Numero_Facture" 'enregistrement
wb.Close
Application.ScreenUpdating = True
MsgBox "Vous êtes maintenant sur le fichier '" & .[F4] & "_service.xlsm'." _
& vbLf & "Dans le dossier '" & Left(nom, Len(nom) - 1) & "'."
End If
End If
End With
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If IsError([FichierCopié]) Then Me.Saved = True 'évite l'invite
End Sub