Sub b()
Dim obj As Object
Dim k%
For Each obj In ThisWorkbook.VBProject.VBComponents
If obj.Type = 3 Then
k = k + 1
MsgBox k & " " & obj.Name
End If
Next obj
End Sub
Sub b()
Dim Usf As Object
Set Usf = Application.VBE.ActiveVBProject.vbcomponents("UserForm1").designer
MsgBox "Usf est un UserForm ? " & TypeOf Usf Is UserForm
MsgBox "Usf.Visible = " & Usf.Visible
End Sub
Sub b()
Dim Usf As Object
Const UsfName = "UserForm1"
Load UserForm1
Load UserForm2
For Each Usf In VBA.UserForms
If Usf.Name = UsfName Then Exit For
Next Usf
If Not Usf Is Nothing Then
MsgBox "Usf est un UserForm ? " & TypeOf Usf Is UserForm
MsgBox "Usf.Visible = " & Usf.Visible
Else
MsgBox UsfName & " non trouvé"
End If
Unload UserForm1
Unload UserForm2
End Sub
Sub b()
Dim Usf As Object
Set Usf = Application.VBE.ActiveVBProject.vbcomponents("UserForm1")
MsgBox TypeName(Usf)
VBA.UserForms.Add (Usf.Name) 'on l'ajoute a la collection forms de vba si l'on veux l'object!!!!! userform!!!!
'ben maintenant c'est facile
'on vient de l'ajouter à la collection ,c'est donc le dernier
Set Usf = UserForms(UserForms.Count - 1)'et oui c'est en base 0
MsgBox TypeName(Usf)
End Sub
Function usfStatus(strUsfName As String) As Byte
'---------------------------------------------------------------------
' Auteur : hasco 14/03/2007 modifiée le 16/10/2022 pour xld
' Fonction : retourne un byte indiquant le status d'un userform
' : 0 = non chargé 2 = chargé 4 = chargé et visible
'----------------------------------------------------------------------
Dim i As Integer
usfStatus = 0
For i = 0 To UserForms.Count - 1
If LCase(UserForms(i).Name) = LCase(strUsfName) Then
usfStatus = 2 + (UserForms(i).Visible * -2)
Exit For
End If
Next i
Oui, ça je m'en doutais Designer -> Mode Création. Mais j'étais intéressé à tout hasard par le TypOf retourné qui était UserForm, et donc pas le bon type suite aux explications de @Dranreb et de @patricktoulon.le mode designer ce n'est que pour le mode edition par vba (donc en écriture seulement)
Et oui @Hasco que je remercie pour ses fonctions, faisait déjà en 2007 (je n'étais alors qu'un jeunot de 54 ans), des opérations complexes pour ses clients.jolie le retour de 3 position en une seule opération
Option Explicit
Sub test()
MsgBox "vous avez cliqué sur " & msg("salut Dudu2")
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