Hello Everybody
I'm trying to create and initialize an userform dynamically. I can create it but not initialize. I'm publishing the code that I'm using
the problem occurs when in Userform_Initialize it doesn't recognize the variable FormName
Some ideas
Thanks
Option Explicit
Public FormName As String
Sub MakeForm()
Dim TempForm As Object ' VBComponent
Dim NewButton As MSForms.CommandButton
Dim TextLocation As Integer
' ** Additional variable
Dim X As Integer
'Locks Excel spreadsheet and speeds up form processing
Application.VBE.MainWindow.Visible = False
Application.ScreenUpdating = False
' Create the UserForm
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
'Set Properties for TempForm
With TempForm
.Properties("caption") = "Menu Principal Louro e Rosmaninho"
.Properties("Width") = Application.Width
.Properties("Height") = Application.Height
.Properties("Left") = Application.Left
End With
FormName = TempForm.Name
With TempForm.CodeModule
' ** Delete This: TextLocation = .CreateEventProc("Click","CommandButton1")
' ** Add/change next 5 lines
' This code adds the commands/event handlers to the form
X = .CountOfLines
.InsertLines X + 1, "Public Sub Userform_Initialize()"
.InsertLines X + 2, "Dim filename As String"
.InsertLines X + 3, "MsgBox FormName"
.InsertLines X + 4, "filename = ""C:\Pessoal\Investimento Mercearia Prata\Excel Tables Files\Imagens\Fundo-Menu-Principal-Mercearia-da-Prata.jpg"""
.InsertLines X + 5, "FormName.Picture = LoadPicture(filename)"
.InsertLines X + 6, "FormName.PictureSizeMode = fmPictureSizeModeStretch"
.InsertLines X + 6, "MsgBox 2 & FormName"
.InsertLines X + 7, "End Sub"
End With
' Show the form
' VBA.UserForms.Add(FormName).Show
VBA.UserForms.Add(FormName).Show
Bookmarks