Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private counter As Integer
- Private Sub UserForm_Initialize()
- counter = 1
- End Sub
- Private Sub btn_AddDualControl_Click()
- ' Increment counter for new textbox naming
- counter = counter + 1
- ' Get reference to last textboxes for positioning
- Dim lastControlBox As MSForms.TextBox
- Dim lastTCBox As MSForms.TextBox
- Set lastControlBox = Me.Controls("txt_DualControl" & (counter - 1))
- Set lastTCBox = Me.Controls("txt_DualTC" & (counter - 1))
- ' Create new textboxes
- Dim newControlBox As MSForms.TextBox
- Dim newTCBox As MSForms.TextBox
- ' Create and set properties for Control description textbox
- Set newControlBox = Me.Controls.Add("Forms.TextBox.1", "txt_DualControl" & counter)
- With newControlBox
- .Left = lastControlBox.Left
- .Top = lastControlBox.Top + lastControlBox.Height + 5
- .Width = lastControlBox.Width
- .Height = lastControlBox.Height
- End With
- ' Create and set properties for Task Card textbox
- Set newTCBox = Me.Controls.Add("Forms.TextBox.1", "txt_DualTC" & counter)
- With newTCBox
- .Left = lastTCBox.Left
- .Top = lastTCBox.Top + lastTCBox.Height + 5
- .Width = lastTCBox.Width
- .Height = lastTCBox.Height
- End With
- ' Add new row to the table in the document
- Dim cc As ContentControl
- Dim tbl As Table
- ' Find the content control by its tag
- For Each cc In ActiveDocument.ContentControls
- If cc.Tag = "box_DualTCs" Then
- Set tbl = cc.Range.Tables(1)
- ' Add new row to the table
- tbl.Rows.Add
- Exit For
- End If
- Next cc
- End Sub
- Public Sub TransferDataToTable()
- Dim cc As ContentControl
- Dim tbl As Table
- Dim i As Integer
- ' Find the content control and its table
- For Each cc In ActiveDocument.ContentControls
- If cc.Tag = "box_DualTCs" Then
- Set tbl = cc.Range.Tables(1)
- ' Loop through all textbox pairs and add their data to the table
- For i = 1 To counter
- Dim controlBox As MSForms.TextBox
- Dim tcBox As MSForms.TextBox
- Set controlBox = Me.Controls("txt_DualControl" & i)
- Set tcBox = Me.Controls("txt_DualTC" & i)
- ' Add data to table
- tbl.Rows(i).Cells(1).Range.Text = controlBox.Text
- tbl.Rows(i).Cells(2).Range.Text = tcBox.Text
- Next i
- Exit For
- End If
- Next cc
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment