Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2017, 01:31 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default 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
Set 2:

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
Reply With Quote
  #2  
Old 11-29-2017, 02:02 PM
macropod's Avatar
macropod macropod is offline How to use checkbox to show/hide bookmarked text? Windows 7 64bit How to use checkbox to show/hide bookmarked text? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You shouldn't need anything more complicated than:
Code:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = CheckBox1.Value
End Sub
That said, toggling the 'hidden' property of a range is unreliable, since whether that range continues to display, or even print when not displayed, is dependent on how the user has Word configured.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-30-2017, 01:05 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 11-30-2017, 01:30 PM
macropod's Avatar
macropod macropod is offline How to use checkbox to show/hide bookmarked text? Windows 7 64bit How to use checkbox to show/hide bookmarked text? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by namrehx View Post
How can i get a multiple bookmarks to hide/appear when I check/uncheck a checkbox?
That's as simple as referencing more bookmarks in the same macro. For example:
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
You can even invert the action, so the hidden state is opposite whatever the checkbox state is:
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
and you can mix the two together:
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]
Reply With Quote
  #5  
Old 11-30-2017, 03:12 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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
The code that you provided makes the bookmark disappear when checking the checkbox. I'm trying to make it so that the bookmark appears when the checkbox is checked and not unchecked.
Code:
Private Sub CheckBox1_Click()
 
ActiveDocument.Bookmarks("Bookmark1").Range.Font.Hidden = CheckBox1.Value

End Sub
Using your coding, how would you make the bookmark appear instead of disappearing when checking the checkbox?
Reply With Quote
  #6  
Old 11-30-2017, 03:13 PM
macropod's Avatar
macropod macropod is offline How to use checkbox to show/hide bookmarked text? Windows 7 64bit How to use checkbox to show/hide bookmarked text? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

As I said:
Quote:
Originally Posted by macropod View Post
You can even invert the action, so the hidden state is opposite whatever the checkbox state is:
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
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-30-2017, 03:27 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

Awesome! That works! I'm able to hide and display multiple bookmarks with just one checkbox. Thank you very much!
Reply With Quote
  #8  
Old 12-11-2017, 12:59 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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
Thanks.
Reply With Quote
  #9  
Old 12-12-2017, 02:21 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 12-12-2017, 02:23 PM
macropod's Avatar
macropod macropod is offline How to use checkbox to show/hide bookmarked text? Windows 7 64bit How to use checkbox to show/hide bookmarked text? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 12-12-2017, 02:41 PM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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!
Reply With Quote
  #12  
Old 12-12-2017, 03:32 PM
macropod's Avatar
macropod macropod is offline How to use checkbox to show/hide bookmarked text? Windows 7 64bit How to use checkbox to show/hide bookmarked text? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 12-13-2017, 06:57 AM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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
Thank you so much! This works for me!
Reply With Quote
  #14  
Old 12-14-2017, 07:25 AM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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..
Reply With Quote
  #15  
Old 12-14-2017, 07:50 AM
namrehx namrehx is offline How to use checkbox to show/hide bookmarked text? Windows 10 How to use checkbox to show/hide bookmarked text? Office 2013
Novice
How to use checkbox to show/hide bookmarked text?
 
Join Date: Nov 2017
Posts: 12
namrehx is on a distinguished road
Default

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?
Reply With Quote
Reply



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
How to use checkbox to show/hide bookmarked text? Show/Hide Text based on Checkbox Selection tammytran105 Word VBA 7 10-02-2014 04:30 PM
How to use checkbox to show/hide bookmarked text? Hide Checkbox When Printing vinceplunkett Word 1 12-03-2013 01:53 AM
How to use checkbox to show/hide bookmarked text? VBA for CheckBox to Hide Slides mutchy25 PowerPoint 1 09-21-2012 01:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:38 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft