6 Replies Latest reply on Dec 4, 2017 2:23 PM by Vasili Delizisis

    vba create code dynamically

    Vasili Delizisis



      I am trying to make a user form where buttons and textbox are created on run time.

      When the macro runs It searches for parts in the assembly and checks for a specific custom property and If true it creates a textbox with the filename and a toggle button next to It. The problem I am having Is that I need to add code for the toggle button on run time as well but I am stuck.


      I have used the code below to insert lines of code but get an error: run-time 438 Object doesn`t support this property or method,

      stops at the line ( With Myuserform.CodeModule)

      I also have the reference Microsoft Visual Basic for Applications Extensibility 5,3 active

      There Is a lot of examples on google ( mostly for excel ) but I cant get them to work with Solidworks .

      Anyone knows what I am doing wrong here or guide me In the right way?


      Dim MyUserform As Object

      Dim X As Integer, MaxWidth As Long



      Set MyUserform = UserForm1



          With MyUserform.CodeModule

              X = .CountOfLines

              .InsertLines X + 1, "Sub togglebutton1_Click()"

              .InsertLines X + 2, " msgbox "Hello World"

              .InsertLines X + 3, "End Sub"



          End With

        • Re: vba create code dynamically
          Fatih Mehmet Ozcan

          Can you make your textbox and button already installed but hidden then get them shown after your necessities are provided?

          You can do this by using Visible property

          UserForm1.TextBox1.Visible = False

            • Re: vba create code dynamically
              Vasili Delizisis



              I could use the .visible property but the problem Is that I have a frame for each sub assemble (forgot to mansion that) and within the frame the part name and toggle button, as there could be a couple of sub assemble and parts It could mean a lot of hazzle of placing them and reorient and sizing the frame depending of how many the are.


              Creating the user form on run time work great but adding code for the buttons Is where I am stuck at


              I have also tried to add the "sub togglebutton1_click()" and then running the micro but once the user form Is being created on run time there Is no click event on the toggle buttons

            • Re: vba create code dynamically
              Ulf Stockburger

              Hi Vasili


              Try that. You have an event handler for all buttons in which you can identify each button you pressed. For text boxes you only have to create a new class similar to BtnClass and copy and modify the sub NewBtn.



              ' <Create a class named BtnClass and insert this code into it>

              Option Explicit


              Public WithEvents BtnEvents As MSForms.CommandButton


              Private Sub BtnEvents_click()

                MsgBox "Click from button " & BtnEvents.Name

              End Sub

              ' <Create a class named BtnClass and insert this code into it>


              ' <Insert this code into the UserForm>

              Option Explicit


              Dim BtnArray() As New BtnClass

              Private Type btnDataType

                  Name As String

                  Width As Integer

                  Height As Integer

                  Left As Integer

                  Top As Integer

              End Type


              Private Sub UserForm_Initialize()

                  Dim i As Integer

                  Dim btn As btnDataType

                  For i = 1 To 3

                      btn.Height = 10

                      btn.Left = (btn.Width) * i

                      btn.Top = 10

                      btn.Width = 10

                      btn.Name = "Button " & i

                      Call NewBtn(btn)

                  Next i

              End Sub


              Private Sub NewBtn(btn As btnDataType)

                  Dim btnData As MSForms.CommandButton

                  Set btnData = Me.Controls.Add("Forms.CommandButton.1")

                  With btnData

                     .Name = btn.Name

                     .Width = btn.Width

                     .Height = btn.Height

                     .Left = btn.Left

                     .Top = btn.Top

                  End With

                  If (Not Not BtnArray) <> 0 Then

                      ReDim Preserve BtnArray(UBound(BtnArray) + 1)


                      ReDim BtnArray(0)

                  End If

                  Set BtnArray(UBound(BtnArray)).BtnEvents = btnData

              End Sub

              ' </Insert this code into the UserForm>


              Hope it helps.


              BiI SAP-PDM