#1
|
|||
|
|||
How to use checkbox to show/hide bookmarked text?
I'm trying to create a document in Word 2016 that will allow a user to have the ability to check a checkbox to make specific bookmarked text content appear within the document and to disappear when unchecked.
I have tried two different sets of VBA code while using an ActiveX Control type checkbox for the UI but nothing is working. To bookmark a set of text, I highlighted the text that I want and added a bookmark named "Bookmark1". I am not a programmer by any means. What am I missing or what can i do to accomplish what I want? I'm trying to do this for multiple checkboxes and bookmarks. Thanks. Set 1: Code:
Private Sub CheckBox1_Click() If Bookmarks("Bookmark1").Range.Font.Hidden = True Then Bookmarks("Bookmark1).Range.Font.Hidden = False Else Bookmarks("Bookmark1).Range.Font.Hidden = True End If End Sub Code:
Private Sub CheckBox1_Click() Dim ShowText As Range Dim HideText As Range Set ShowText = ActiveDocument.Bookmarks("Bookmark1").Range Set HideText = ActiveDocument.Bookmarks("Bookmark1").Range If CheckBox1.Value = True Then ShowText.Font.Hidden = False HideText.Font.Hidden = True Else ShowText.Font.Hidden = True HideText.Font.Hidden = False End If End Sub |
#2
|
||||
|
||||
You shouldn't need anything more complicated than:
Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = CheckBox1.Value End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks. I got it working where I can get a bookmark to hide/appear when I check/uncheck a checkbox.
I have different text paragraphs spread out within my document that I want associated with a checkbox. How can i get a multiple bookmarks to hide/appear when I check/uncheck a checkbox? Or can I only use one checkbox to hide/show one bookmark at a time? |
#4
|
||||
|
||||
Quote:
Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = CheckBox1.Value ActiveDocument.Bookmarks("Bookmark2").Range.Font.Hidden = CheckBox1.Value ActiveDocument.Bookmarks("Bookmark3").Range.Font.Hidden = CheckBox1.Value End Sub Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = Not CheckBox1.Value ActiveDocument.Bookmarks("Bookmark2").Range.Font.Hidden = Not CheckBox1.Value ActiveDocument.Bookmarks("Bookmark3").Range.Font.Hidden = Not CheckBox1.Value End Sub Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = CheckBox1.Value ActiveDocument.Bookmarks("Bookmark2").Range.Font.Hidden = CheckBox1.Value ActiveDocument.Bookmarks("Bookmark3").Range.Font.Hidden = Not CheckBox1.Value End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi,
What I had working for a single checkbox and bookmark before needing help with multiple bookmarks for one checkbox was this: Code:
Private Sub CheckBox1_Click() Dim ShowText As Range Dim HideText As Range Set ShowText = ActiveDocument.Bookmarks("Bookmark1").Range Set HideText = ActiveDocument.Bookmarks("Bookmark1").Range If CheckBox1.Value = True Then ShowText.Font.Hidden = True HideText.Font.Hidden = False Else ShowText.Font.Hidden = False HideText.Font.Hidden = True End If End Sub Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = CheckBox1.Value End Sub |
#6
|
||||
|
||||
As I said:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Awesome! That works! I'm able to hide and display multiple bookmarks with just one checkbox. Thank you very much!
|
#8
|
|||
|
|||
Hi,
Need more help. How do i make a bookmark that is tied to multiple checkboxes, stay displayed if at least one of the checkbox that is tied to it is checked? Look below at the following scenarios: What is currently happening: 1) Checkbox1 is checked 2) Bookmark1 appears 3) Checkbox2 is checked 4) Bookmark1 stays displayed 5) Checkbox1 is unchecked 6) Bookmark1 disappears What I want to happen: 1) Checkbox1 is checked 2) Bookmark1 appears 3) Checkbox2 is checked 4) Bookmark1 stays displayed 5) Checkbox1 is unchecked 6) Bookmark1 stays displayed (because checkbox2 is still checked) Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = Not CheckBox1.Value End Sub Private Sub CheckBox2_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = Not CheckBox2.Value End Sub |
#9
|
|||
|
|||
What I am trying to say is that i am having problems in working with multiple checkboxes for the same bookmark.
I have two checkboxes, CheckBox1 and CheckBox2. Both checkboxes, when checked individually, will show the same bookmarked text called Bookmark1. Say that I check both of them and then later decide to uncheck one of them, the bookmark will then hide. I want the bookmark to still be showing since the other checkbox is still checked. The bookmark should only hide when both checkboxes are unchecked. How can i re-code this or does VBA not have this capability? Thanks. Code:
Private Sub CheckBox1_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = Not CheckBox1.Value End Sub Private Sub CheckBox2_Click() ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = Not CheckBox2.Value End Sub |
#10
|
||||
|
||||
Since you have two checkboxes referring to the same bookmark, you can't really expect anything else; whichever one you check/uncheck last will take precedence. You should ask yourself why you have two checkboxes acting on the same bookmark and whether that's really necessary. If it really is necessary, then you're going to have to work out the logic for each/either checkbox to check the other's state before changing the bookmark state.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
What I'm trying to accomplish is to make each checkbox represent a product. The Bookmarked text will then represent a specific requirement for the product. Depending on what products are being purchased, will then determine what specific requirements are shown. Some products will have the same requirement. In this case, product CheckBox1 and CheckBox2 both have the same requirement, Bookmark1. Both products can be purchased individually or bundled together. Whether both products are purchased individually or together, they will always have the same requirement of Bookmark1. Therefore, If a user selects both products but then decides that only one product needs to be selected, the requirement should still be showing.
I'm not a programmer by any means. If you can help me get started as this seems like it'll take more advanced logic, it would be greatly appreciated! |
#12
|
||||
|
||||
In that case, try:
Code:
Private Sub CheckBox1_Click() Call ShowHide End Sub Private Sub CheckBox2_Click() Call ShowHide End Sub Sub ShowHide() Dim bVis As Boolean: bVis = True If CheckBox1.Value = True Then bVis = False If CheckBox2.Value = True Then bVis = False ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = bVis End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Quote:
|
#14
|
|||
|
|||
Need help with another thing..
We were able to make it so that the bookmark doesn't go away when more than one checkbox acting on the same bookmark is checked. Now, how do we make it so that a bookmark only appears when two or more bookmark is checked? Adding on to my example above about products and requirements: I have an additional feature called CheckBox3 that I can add to products CheckBox1 and/or CheckBox2. I have an additional requirement that comes with CheckBox3 called Bookmark2. The thing is that Bookmark2 should only appear when Checkbox3 is checked AND when CheckBox1 and/or CheckBox2 is checked. Everything else that we did still need to apply as well.. |
#15
|
|||
|
|||
Also, How do I do the same thing that the code you provided does but for a whole different set of checkboxes and bookmarks within the same document? For example, I got a CheckBox4, CheckBox5 , and Bookmark3 that's completely separate from the first two checkboxes and bookmark. I'm getting an ambiguous error when trying to do this. I know it's because I'm using the ShowHide method more than once. WHat's the solution?
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Hide rows with checkbox | gebobs | Excel | 3 | 03-24-2015 12:05 PM |
Show/hide text macro and security issues? | subspace3 | Word VBA | 1 | 10-05-2014 10:05 AM |
Show/Hide Text based on Checkbox Selection | tammytran105 | Word VBA | 7 | 10-02-2014 04:30 PM |
Hide Checkbox When Printing | vinceplunkett | Word | 1 | 12-03-2013 01:53 AM |
VBA for CheckBox to Hide Slides | mutchy25 | PowerPoint | 1 | 09-21-2012 01:40 AM |