0

Looking for a more customizable MsgBox, I read some users that suggested to others with my same problem: build a form "on the fly"! Well. That's what I do.
Unfortenately my code run, my form's shown, but suddently disappear.
Only fews 10/secs, maybe 100/secs remains open.
No errors.
In VBE form exist and if I run it from project browser, all is ok, form remain open till I click on OK (Unload form) or close it from the X.
I don't understand why.
I got Windows 11 x64, Office 2021 x32. I'm working in my PERSONALS.XLSB so my "custom MsgBox" is enabled in all my others XLSM; I declared a Public Sub for the same reason.
Here's my code:

Option Explicit
Public Sub BuildFrmOnTheFly(ByVal strFrmTitle As String, ByVal strFrmTxt As String)

' GestErr.
On Error GoTo GesErr

Dim VBComp As Object
Dim frmZZZ As Object
Dim txtZZZ As MSForms.TextBox
Dim btnZZZ As MSForms.CommandButton
    
    ' If a FORM named frmZZZ exist, delete!
    For Each VBComp In ThisWorkbook.VBProject.VBComponents
        With VBComp
            If .Type = 3 Then
                If .Name = "frmZZZ" Then
                    ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents("frmZZZ")
                End If
            End If
        End With
    Next VBComp
    
    ' Save file if isn't.
    If Application.Workbooks("PERSONAL.XLSB").Saved = False Then
        Application.DisplayAlerts = False
        Application.Workbooks("PERSONAL.XLSB").Save
        Application.DisplayAlerts = True
    End If

    ' Hide VBE win.
    Application.VBE.MainWindow.Visible = False

    ' Add and build Form frmZZZ.
    Set frmZZZ = ThisWorkbook.VBProject.VBComponents.Add(3)
    With frmZZZ
        .Properties("BackColor") = RGB(255, 255, 255)
        .Properties("BorderColor") = RGB(64, 64, 64)
        .Properties("Caption") = strFrmTitle
        .Properties("Height") = 150
        .Properties("Name") = "frmZZZ"
        .Properties("ShowModal") = False
        .Properties("Width") = 501
    End With

    ' Build TextBox txtZZZ.
    Set txtZZZ = frmZZZ.Designer.Controls.Add("Forms.TextBox.1")
        With txtZZZ
            .Name = "txtZZZ"
            .BorderStyle = fmBorderStyleNone
            .BorderColor = RGB(169, 169, 169)
            .font.Name = "Calibri"
            .font.Size = 12
            .ForeColor = RGB(70, 70, 70)
            .SpecialEffect = fmSpecialEffectFlat
            .MultiLine = True
            .Left = 0
            .Top = 10
            .Height = 75
            .Width = 490
            .text = strFrmTxt
        End With

    ' Build Button btnZZZ (OK)
    Set btnZZZ = frmZZZ.Designer.Controls.Add("Forms.commandbutton.1")
        With btnZZZ
            .Name = "btnZZZ"
            .Caption = "OK"
            .Accelerator = "M"
            .Top = 90
            .Left = 0
            .Width = 70
            .Height = 20
            .font.Size = 12
            .font.Name = "Calibri"
            .BackStyle = fmBackStyleOpaque
        End With
    
    ' Add module to the Form.
    With frmZZZ.CodeModule
        ' Initialize Form.
        .InsertLines .CountOfLines + 1, "Private Sub UserForm_Initialize()"
        .InsertLines .CountOfLines + 1, "Dim TopOffset As Integer"
        .InsertLines .CountOfLines + 1, "Dim LeftOffset As Integer"
        .InsertLines .CountOfLines + 1, "    TopOffset = (Application.UsableHeight / 2) - (frmZZZ.Height / 2)"
        .InsertLines .CountOfLines + 1, "    LeftOffset = (Application.UsableWidth / 2) - (frmZZZ.Width / 2)"
        .InsertLines .CountOfLines + 1, "    frmZZZ.Top = Application.Top + TopOffset"
        .InsertLines .CountOfLines + 1, "    frmZZZ.Left = Application.Left + LeftOffset"
        .InsertLines .CountOfLines + 1, "    txtZZZ.WordWrap = True"
        .InsertLines .CountOfLines + 1, "    txtZZZ.MultiLine = True"
        .InsertLines .CountOfLines + 1, "    txtZZZ.font.Size = 12"
        .InsertLines .CountOfLines + 1, "    txtZZZ.Left = (frmZZZ.InsideWidth - txtZZZ.Width) / 2"
        .InsertLines .CountOfLines + 1, "    btnZZZ.Left = (frmZZZ.InsideWidth - btnZZZ.Width) / 2"
        .InsertLines .CountOfLines + 1, "End Sub"

        ' Terminate Form.
        .InsertLines .CountOfLines + 1, "Private Sub UserForm_Terminate()"
        ' Remove Form from VBA Proj.
        .InsertLines .CountOfLines + 1, "    ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents(""frmZZZ"")"
        .InsertLines .CountOfLines + 1, "    Application.VBE.MainWindow.Visible = True"
        .InsertLines .CountOfLines + 1, "End Sub"

        ' Btn OK close Form.
        .InsertLines .CountOfLines + 1, "Private Sub btnZZZ_Click()"
        .InsertLines .CountOfLines + 1, "   Unload Me"
        .InsertLines .CountOfLines + 1, "End Sub"
    
    End With
    
    ' Add Form frmZZZ and show it.
    Set frmZZZ = VBA.UserForms.Add("frmZZZ")
    frmZZZ.Show
    
' Exit sub, before empty vars.
Uscita: strFrmTitle = Empty
        strFrmTxt = Empty
        Set btnZZZ = Nothing
        Set txtZZZ = Nothing
        Set frmZZZ = Nothing
        Exit Sub
' If error comes.
GesErr: MsgBox "Error in Sub" & vbCrLf & "'BuildFrmOnTheFly'" & vbCrLf & vbCrLf & Err.Description
        Resume Uscita
' End.
End Sub

And here is how I call it:

Option Explicit
Sub TryBuildFrmOnTheFly()
Dim strText As String
    strText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut MQ" '95 chars
    Call BuildFrmOnTheFly("This is the form title", strText)
End Sub

Problem seems to be when I start filling

With frZZZ.CodeModule
....
End With

Already a simple Button like btnZZZ

        .InsertLines .CountOfLines + 1, "Private Sub btnZZZ_Click()"
        .InsertLines .CountOfLines + 1, "   Unload Me"
        .InsertLines .CountOfLines + 1, "End Sub"

Give me the problem.
I've already read post like this or this, but nothing to do. Wait for You, more experts than me, thanks in advance.

Emanuele Tinari
  • 120
  • 1
  • 2
  • 10
  • I've read through this several times and can't seem to understand what you are trying to do and what the problem is. Can you trying explaining it again and try to eliminate the misspellings and typos. – SixSigmaGuy Feb 28 '23 at 12:12
  • 1
    I'm trying to add a Form and It's code programmatically from my VBA Sub BuildFrmOnTheFly. The form appears, his code too. But after frmZZZ.Show, the Form close after few dec/second, maybe 1 second. Like I close it with the Close button X. – Emanuele Tinari Mar 15 '23 at 22:00

1 Answers1

0

After show, add the parameter VBModal

frmZZZ.Show vbModal

SixSigmaGuy
  • 313
  • 3
  • 11