Private Sub Workbook_BeforeClose(cancel As Boolean)
Dim Rep As String, Fich1 As String, c As Byte, VBC As Object
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Rep = ActiveWorkbook.Path & "\"
Fich1 = Sheets("WORKSCOPE").Range("C1") & ".xls"
For c = 1 To Len(Fich1) 'test caractères interdits
If InStr("\/:*?""""<>|", Mid(Fich1, c, 1)) > 0 Then
MsgBox "Le nom en C1 contient des caractères interdits !"
cancel = True
Exit Sub
End If
Next
If Dir(Rep & Fich1) <> "" Then 'test existence fichier
Q = MsgBox(Fich1 & " existe déjà, voulez-vous le remplacer ?", vbYesNo)
If Q = 7 Then GoTo Ligne1 Else GoTo Ligne2
Else: GoTo Ligne2
End If
Ligne1:
cancel = True
Exit Sub
Ligne2:
ActiveWorkbook.SaveAs Rep & Fich1
With ActiveWorkbook.VBProject 'suppression code
For Each VBC In .VBComponents
If VBC.Type = 100 Then
With VBC.CodeModule
.DeleteLines 1, .CountOfLines
.CodePane.Window.Close
End With
Else
.VBComponents.Remove VBC
End If
Next VBC
End With
SendKeys "%O" 'enregistrement
End Sub