#1
|
|||
|
|||
UserForm with checkboxes that hide/show bookmarked text in document
Hi,
I am very new to UserForms (control fields, VBA, etc.) I have created a UserForm for a document with a number of text fields and then three checkboxes. The intention of the three checkboxes is to link each one to different bookmarked text within the document. The text needs to be shown/hidden depending on whether or not the checkbox is checked. My userform has a 'submit' button. I have got the code for filling the text fields and it's working great. I'm stuck on how to structure the If statements. Below is a copy of my code for the 'submit' button on my userform. Private Sub SubmitButton_Click() Dim Participant As Range Set Participant = ActiveDocument.Bookmarks("Participant").Range Participant.Text = Me.ParticipantName.Value Dim DateofBirth As Range Set DateofBirth = ActiveDocument.Bookmarks("DateofBirth").Range DateofBirth.Text = Me.DOBText.Value Dim NDISnumber As Range Set NDISnumber = ActiveDocument.Bookmarks("NDISnumber").Range NDISnumber.Text = Me.NDISText.Value Dim ClientNominee As Range Set ClientNominee = ActiveDocument.Bookmarks("ClientNominee").Range ClientNominee.Text = Me.ClientText.Value Dim EffectiveStartDate As Range Set EffectiveStartDate = ActiveDocument.Bookmarks("EffectiveStartDate").Ran ge EffectiveStartDate.Text = Me.EffectiveText.Value Dim EffectiveEndDate As Range Set EffectiveEndDate = ActiveDocument.Bookmarks("EffectiveEndDate").Range EffectiveEndDate.Text = Me.UntilText.Value If ContinenceCB Then ActiveDocument.Bookmarks("ContinenceCost") = True Else: ActiveDocument.Bookmarks("ContinenceCost") = True Me.Repaint UserForm1.Hide I know that the If statement above is incorrect. I've attached a word document that better explains what I need to show in the completed document - based on checkbox selection. Any help with this is greatly appreciated. PS (Sorry if the If statement is total rubish. I'm quite confused at the moment.) |
#2
|
||||
|
||||
Firstly, writing to bookmarks is non-intuitive because the added text is beside the bookmark rather than inside it. This means it works once but a second pass with the macro adds a second copy instead of replacing. You get around this shortcoming by redefining the bookmark range after writing the text. See https://www.msofficeforums.com/word-...lues-text.html for an example of this.
If you want to show/hide a range based on a checkbox, you can do it without an If statement Code:
' Hide the text if checked, show if unchecked ActiveDocument.Bookmarks("ContinenceCost").Range.Font.Hidden = ContinenceCB 'or vice versa ActiveDocument.Bookmarks("ContinenceCost").Range.Font.Hidden = Not ContinenceCB
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you for your speedy response. I know I'm confusing how to do things on userforms and within documents using controls.
I am very new to all of this and trying to work things out very quickly. As I have three checkboxes and three bookmarked bits of text (one for each checkbox). I get that I'll need to do this for each one. What I don't know is where or how I do that. Does it go within the code that runs when the user form is submitted? If yes, how to I reference the specific checkbox and will it work for me to have three pieces of code that are the same but refer to different checkboxes and bookmarked text? Or does it go against the properties of the checkbox on the userform? If yes, How? I've looked through the grid that comes up for the checkbox properties on the userform and I can't find where I would put the code. Sorry these are such basic questions. As I mentioned I a total beginner (cobbling things together through youtube tutorials). Thanks again for your help Last edited by dohertym; 05-16-2022 at 04:30 PM. Reason: To make it clearer |
#4
|
|||
|
|||
Actually.
It's all good. I have added it toto the macro that runs when the user form is closed. It worked perfectly. Thank you so much |
#5
|
||||
|
||||
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 |
Tags |
checkbox, show, userform |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Show Userform hide active document only | Just Learning | Word VBA | 4 | 03-25-2020 08:57 PM |
Auto-populating a document pulling data from external doc using a Userform with Checkboxes | kateabode | Word VBA | 10 | 10-12-2018 06:56 AM |
How to use checkbox to show/hide bookmarked text? | namrehx | Word VBA | 16 | 12-14-2017 01:45 PM |
How to use checkbox to show and hide bookmarked text? | namrehx | Word VBA | 1 | 12-12-2017 02:17 PM |
Show userform without losing document focus? Or other method to get a graphic to pop? | AlexR | Word VBA | 7 | 03-31-2013 12:17 PM |