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
|
Sub AjoutError()
Dim m As Module, ligne() As String, s As String
Dim i As Long, j As Long, n As Long
s = InputBox("Nom du module", "Nom du module")
If s = "" Then Exit Sub
DoCmd.OpenModule s
Set m = Modules(s)
ligne = Split(m.Lines(1, m.CountOfLines), vbCrLf)
j = UBound(ligne)
Do Until j < 1 'on part de la fin du module
If (Left$(ligne(j), 7) = "End Sub") Then 'est-ce une fin de procedure?
m.InsertLines j + 1, "Exit Sub" & vbCrLf & "ErrHandler:" & vbCrLf & "MsgBox(""Erreur:"" & Error )"
End If
If (Left$(ligne(j), 12) = "End Function") Then 'est-ce une fin de fonction?
m.InsertLines j + 1, "Exit Function" & vbCrLf & "ErrHandler:" & vbCrLf & "MsgBox(""Erreur:"" & Error )"
End If
If (Left$(ligne(j), 4) = "Sub ") Then 'est-ce un début de procedure
m.InsertLines j + 2, "on error goto ErrHandler"
End If
If (Left$(ligne(j), 9) = "Function ") Then 'st-ce un début de fonction
If Right$(ligne(j), 1) <> "_" Then
m.InsertLines j + 2, "on error goto ErrHandler"
Else 'les paramètres de la fonction sont sur plusieurs lignes
n = j
Do Until Right$(ligne(n), 1) <> "_"
n = n + 1
Loop
m.InsertLines n + 2, "on error goto ErrHandler"
End If
End If
j = j - 1
Loop
DoCmd.Close acModule, s
End Sub |
Partager