Dim AutreQueG39G40 As Boolean, numero$ 'mémorisation des variables
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
Dim a$, nom$, i As Byte
If Sh.Name <> "Facture de service" Then Exit Sub
'---initialise AutreQueG39G40---
a = Source.Address(0, 0)
If a <> "G39" And a <> "G40" And a <> "G39:G40" And a <> "G39,G40" And a <> "G40,G39" _
Then AutreQueG39G40 = True
If Intersect(Source, Sh.[F4,D10]) Is Nothing Then Exit Sub
Application.EnableEvents = False
'---interdit la modification du numéro de facture---
Sh.[F4] = numero
'---supprime les caractères interdits---
nom = Sh.[D10]
For i = 1 To 9
nom = Replace(nom, Mid("\/:*?""<>|", i, 1), "")
Next
Sh.[D10] = nom
Application.EnableEvents = True
End Sub
Private Sub Workbook_Open()
Sheets("Facture de service").Activate 'au cas où...
numero = "'" & [F4]
If Not IsError([FichierCopié]) Then Exit Sub 'neutralisation
Dim chemin$, nomfich$, n As Variant
Sheets("Facture de service").Activate 'au cas où...
Application.EnableEvents = False
'---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
Application.EnableEvents = True
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$, Nmodif%, Tmodif$, 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
Application.EnableEvents = False
On Error Resume Next
MkDir chemin & nom 'création du sous-dossier
'---numero de la modification---
If nom = "" And AutreQueG39G40 Then Nmodif = [FichierCopié] + 1
If Nmodif Then Tmodif = "-" & Chr(64 + Nmodif)
If [G39] <> 0 Or [G40] <> 0 Then Tmodif = Tmodif & "-Facture"
[E1] = IIf([G39] = 0 And [G40] = 0, "SOUMISSION", "FACTURE")
Me.Names.Add "FichierCopié", Nmodif, Visible:=False 'nom défini masqué
AutreQueG39G40 = False 'RAZ
'---création/modification du fichier facture .pdf---
.ExportAsFixedFormat xlTypePDF, chemin & nom & .[F4] & Tmodif
'---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] & Tmodif 'enregistrement
wb.Close
'---création/modification du fichier service.xlsm---
Me.SaveAs chemin & nom & .[F4] & "_service", 52 'enregistrement
'---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
Application.EnableEvents = True
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