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
| 'Création de la fonction msgbox personnalisé
Function mDFmsgBox(Titre$, Message$, Align As Byte, Police$, TailleCaract As Byte, ParamArray B()) As Byte
Dim USF As Object
Dim btnB As MSForms.CommandButton
Dim lblM As MSForms.Label
Dim LngMaxB As Integer, Marge As Integer, NbC As Integer
Dim i As Byte
'Création du USF
Set USF = ThisWorkbook.VBProject.VBComponents.Add(3)
'Titre
USF.Properties("Caption") = Titre
'Zone de message
Set lblM = USF.Designer.Controls.Add("Forms.Label.1")
With lblM
.Move 0, 15, 1000, 1000
.WordWrap = False
.Font.Size = TailleCaract
.Font.Name = Police
.Caption = Message
.AutoSize = True
End With
'Boutons
For i = 0 To UBound(B)
Set btnB = USF.Designer.Controls.Add("Forms.CommandButton.1")
With btnB
.AutoSize = True
.Caption = B(i)
LngMaxB = Application.Max(LngMaxB, .Width)
.AutoSize = False
End With
Next i
'Mise en place des contrôles dans le USF
With lblM
USF.Properties("Width") = Application.Max((LngMaxB + 10) * (UBound(B) + 1) + 5, _
.Width + 20)
USF.Properties("Height") = 85 + .Height
.AutoSize = False
.Move 10, 15, USF.Properties("Width") - 20, .Height
.TextAlign = Align
End With
Marge = (USF.Properties("Width") - (LngMaxB + 5) * (UBound(B) + 1)) / 2
For i = 0 To UBound(B)
With USF
.Designer.Controls("CommandButton" & i + 1).Move Marge + (LngMaxB + 5) * i, _
lblM.Top + lblM.Height + 15, LngMaxB, 20
'Procédures évènementielles liées aux boutons
With .CodeModule
.InsertLines .CountOfLines + 1, "Sub CommandButton" & i + 1 & "_Click():VmsgBox =" _
& i + 1 & " :Unload Me:End Sub"
End With
End With
Next i
'Empêche fermeture par la croix
With USF.CodeModule
.InsertLines .CountOfLines + 1, "Private Sub UserForm_QueryClose(Cancel As Integer," _
& " CloseMode As Integer):Cancel = CloseMode = 0:End Sub"
End With
'Affiche, puis détruit le USF
VBA.UserForms.Add(USF.Name).Show
ThisWorkbook.VBProject.VBComponents.Remove USF
mDFmsgBox = VmsgBox
End Function |
Partager