JustJoe21

Untitled

Dec 26th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. Private counter As Integer
  2.  
  3. Private Sub UserForm_Initialize()
  4. counter = 1
  5. End Sub
  6.  
  7. Private Sub btn_AddDualControl_Click()
  8. ' Increment counter for new textbox naming
  9. counter = counter + 1
  10.  
  11. ' Get reference to last textboxes for positioning
  12. Dim lastControlBox As MSForms.TextBox
  13. Dim lastTCBox As MSForms.TextBox
  14. Set lastControlBox = Me.Controls("txt_DualControl" & (counter - 1))
  15. Set lastTCBox = Me.Controls("txt_DualTC" & (counter - 1))
  16.  
  17. ' Create new textboxes
  18. Dim newControlBox As MSForms.TextBox
  19. Dim newTCBox As MSForms.TextBox
  20.  
  21. ' Create and set properties for Control description textbox
  22. Set newControlBox = Me.Controls.Add("Forms.TextBox.1", "txt_DualControl" & counter)
  23. With newControlBox
  24. .Left = lastControlBox.Left
  25. .Top = lastControlBox.Top + lastControlBox.Height + 5
  26. .Width = lastControlBox.Width
  27. .Height = lastControlBox.Height
  28. End With
  29.  
  30. ' Create and set properties for Task Card textbox
  31. Set newTCBox = Me.Controls.Add("Forms.TextBox.1", "txt_DualTC" & counter)
  32. With newTCBox
  33. .Left = lastTCBox.Left
  34. .Top = lastTCBox.Top + lastTCBox.Height + 5
  35. .Width = lastTCBox.Width
  36. .Height = lastTCBox.Height
  37. End With
  38.  
  39. ' Add new row to the table in the document
  40. Dim cc As ContentControl
  41. Dim tbl As Table
  42.  
  43. ' Find the content control by its tag
  44. For Each cc In ActiveDocument.ContentControls
  45. If cc.Tag = "box_DualTCs" Then
  46. Set tbl = cc.Range.Tables(1)
  47. ' Add new row to the table
  48. tbl.Rows.Add
  49. Exit For
  50. End If
  51. Next cc
  52. End Sub
  53.  
  54. Public Sub TransferDataToTable()
  55. Dim cc As ContentControl
  56. Dim tbl As Table
  57. Dim i As Integer
  58.  
  59. ' Find the content control and its table
  60. For Each cc In ActiveDocument.ContentControls
  61. If cc.Tag = "box_DualTCs" Then
  62. Set tbl = cc.Range.Tables(1)
  63.  
  64. ' Loop through all textbox pairs and add their data to the table
  65. For i = 1 To counter
  66. Dim controlBox As MSForms.TextBox
  67. Dim tcBox As MSForms.TextBox
  68.  
  69. Set controlBox = Me.Controls("txt_DualControl" & i)
  70. Set tcBox = Me.Controls("txt_DualTC" & i)
  71.  
  72. ' Add data to table
  73. tbl.Rows(i).Cells(1).Range.Text = controlBox.Text
  74. tbl.Rows(i).Cells(2).Range.Text = tcBox.Text
  75. Next i
  76.  
  77. Exit For
  78. End If
  79. Next cc
  80. End Sub
Advertisement
Add Comment
Please, Sign In to add comment