View Single Post
 
Old 05-16-2022, 09:48 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,988
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Lets do this as efficiently as possible. You should replace each of the bookmarks where TEXT from the userform needs to go with a plain text content control. I assume your table rows already have the bookmarked names that you showed in the document you attached earlier.

Follow these steps:
1. Put a Plain Text Content Control into your document where the userform text fields need their data to go. Set the Title property to a logical name (eg Participant, DOB, NDIS, Client, Start, End)
2. With each corresponding TextBox on your userform, edit its Tag property to use the same word as the Title property you assigned in step 1
3. Edit the Tag property on each CheckBox to match the names of the bookmarked table rows (ie ContinenceCost, EpilepsyCost, WoundCost) that they are supposed to hide.
4. Put this code in the UserForm (to replace the code you posted)
Code:
Private Sub UserForm_Initialize()
  'This pre-fills the userform with existing settings from the document
  Dim aCtl As Control, sTag As String, aCC As ContentControl
  For Each aCtl In Me.Controls
    sTag = aCtl.Tag
    Select Case TypeName(aCtl)
      Case "TextBox"
        If ActiveDocument.SelectContentControlsByTitle(sTag).Count > 0 Then
          aCtl = ActiveDocument.SelectContentControlsByTitle(sTag)(1).Range.Text
        End If
      Case "CheckBox"
        If ActiveDocument.Bookmarks.Exists(sTag) Then
          If ActiveDocument.Bookmarks(sTag).Range.Font.Hidden Then
            aCtl.Value = False
          Else
            aCtl.Value = True
          End If
        End If
    End Select
  Next aCtl
End Sub

Private Sub SubmitButton_Click()
  'This writes the values to the document and hides the userform.
  Dim aCtl As Control, sTag As String, aCC As ContentControl
  For Each aCtl In Me.Controls
    sTag = aCtl.Tag
    Select Case TypeName(aCtl)
      Case "TextBox"
        For Each aCC In ActiveDocument.SelectContentControlsByTitle(sTag)
          aCC.Range.Text = aCtl
        Next aCC
      Case "CheckBox"
        If ActiveDocument.Bookmarks.Exists(sTag) Then
          ActiveDocument.Bookmarks(sTag).Range.Font.Hidden = Not aCtl
        End If
    End Select
  Next aCtl
  Me.Hide
  Unload Me
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote