'**********************************
' MsgBox perso avec userform dynamique
'auteur patricktoulon
'date :12/08/2016
'***********************************
Option Explicit
Sub test()
MsgBox "vous avez cliqué sur " & msg("salut RYU comment va tu ?")
End Sub
Function msg(texte)
Dim Obj As Object, usf
Dim j As Integer
Set usf = ThisWorkbook.VBProject.VBComponents.Add(3)
With usf: .Properties("Caption") = "msgboxAA": .Properties("Width") = 250: .Properties("Height") = 150: End With
Set Obj = usf.Designer.Controls.Add("forms.TextBox.1", "content")
With Obj:
.Left = 0:
.Top = 0:
.Width = usf.Properties("InsideWidth"):
.Height = usf.Properties("Insideheight") - 25
.Name = "content":
.BackColor = &H80C0FF:
.ForeColor = vbGreen
.Font.Name = "algerian"
.Font.Size = 16
.TextAlign = 2
.MultiLine = True
'et toutes autre propriété des textboxs font,borderstyle,etc......
.Value = texte
End With
Set Obj = usf.Designer.Controls.Add("forms.CommandButton.1", "boutonOK")
With Obj:
.Left = usf.Properties("Width") - 60
.Top = usf.Properties("Height") - 25 - 20
.Width = 50
.Height = 20
.Name = "bouttonOK":
.BackColor = vbRed
.ForeColor = vbGreen
.Caption = "OK"
End With
Set Obj = usf.Designer.Controls.Add("forms.CommandButton.1", "boutoncancel")
With Obj:
.Left = usf.Properties("Width") - 120
.Top = usf.Properties("Height") - 25 - 20
.Width = 50
.Height = 20
.Name = "boutoncancel":
.BackColor = vbBlue
.ForeColor = vbMagenta
.Caption = "ANNULER"
End With
'creation insertion code du des evenements
With usf.CodeModule
j = .CountOfLines
.insertlines j + 1, "public reponse"
.insertlines j + 2, "Private Sub bouttonOK_Click():reponse = ""ok"": Me.Hide:End Sub"
.insertlines j + 3, "Private Sub boutoncancel_Click():reponse = ""Annuler"":me.hide:End Sub"
.insertlines j + 4, "Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)"
.insertlines j + 5, "If CloseMode = 0 Then Cancel = True: Me.Hide"
.insertlines j + 6, "End Sub"
End With
VBA.UserForms.Add (usf.Name)
'affichage du pseudo msgbox
With UserForms(UserForms.Count - 1)
.Show
msg = .reponse
End With
ThisWorkbook.VBProject.VBComponents.Remove (usf)
End Function