#1
|
|||
|
|||
Show/Hide Text based on Checkbox Selection
Hi,
I am trying to show/hide text based on whether a checkbox is selected. I was able to get this to work through using VBA and bookmarks, however, I need to have several checkboxes which have their own separate text to be shown if selected. This is where the problem is.. Each time one of the checkboxes if selected, ALL hidden text (which I've created separate bookmarks for) show instead of just the one dependent on what's been called out in the VBA code. Any help would be greatly appreciated!! This is the VBA I used for 5 checkboxes: Code:
Sub CheckBox2_Change() Call ShowHideBookmark End Sub ' Sub ShowHideBookmark() Dim orange As Range Set orange = ActiveDocument.Bookmarks("mytext2").Range If CheckBox2.Value = True Then With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = True End With Else With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = False End With End If End Sub ' Sub CheckBox3_Change() Call ShowHideBookmark3 End Sub ' Sub ShowHideBookmark3() Dim orange As Range Set orange = ActiveDocument.Bookmarks("mytext3").Range If CheckBox3.Value = True Then With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = True End With Else With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = False End With End If End Sub ' Sub CheckBox4_Change() Call ShowHideBookmark4 End Sub ' Sub ShowHideBookmark4() Dim orange As Range Set orange = ActiveDocument.Bookmarks("mytext4").Range If CheckBox4.Value = True Then With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = True End With Else With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = False End With End If End Sub Last edited by macropod; 09-29-2014 at 08:53 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
There are numerous problems with this approach:
1. Your approach modifies Word's .ShowHiddenText property, which necessarily applies to all documents. You cannot use it to apply to just one document or even part of one document. In any event, this doesn't stop such text appearing on printouts. 2. Trying to control document content by hiding/unhiding text, which can be done for just a nominated range (e.g. a bookmarked range) is quite unreliable. That's because whether the text displays or prints (even if not displayed) are controlled by Word configuration settings that are independent of the document. You would have to manipulate those settings as well. When you do this, you need to both trap the user's original configuration and switch back to that whenever they switch to another document or close yours; otherwise, you risk compromising they way they want their systems to work. For proper control, you need to conditionally add/delete the variable content or vary its display/non-display via field coding. PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
If you are going the macro route, then rather than show/hide the hidden text, use the macro to write the text to a bookmarked location or write a nullstring to the bookmark according to the value of the checkbox, then reset the bookmark to the range.
See http://www.gmayor.com/SelectFile.htm for some other ideas.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
Thank you for the quick replies Paul and Graham. I am new to using the developer tab in Word and did not realize it would be an issue to do it the way I did. I'm glad you told me this. I will try to take a different route for creating my form. I tried doing the DocVariable Field thing from Graham's link, but I wasn't able to get it to work.
What would be the best way to create a form like this and not run into any of the problems mentioned by Paul? It does not have to be through Word. If anyone has any samples of a form with hidden text dependent on selections, it would be very helpful to see. Please share if you have one.. thanks!! |
#5
|
||||
|
||||
Without actually seeing a document in which you've tried to implement it, we can't really advise on where you might have gone wrong. Can you attach a document to a post with some representative data (delete anything sensitive), including the macro code? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
||||
|
||||
See the attached which uses doc variables at their simplest.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Attached is the form with my vba in it. It works, but not properly. Upon selecting the "yes" checkbox in question #4, the hidden text appears but also shows an error for the second line of hidden text that should only be shown if the "Regional" checkbox in question #5 is selected. Each line of hidden text should only be associated with it's own checkbox. How could I make this work?
Another question is if I were to create my form this way through imbedding Doc Variables and macros, will it always work no matter who opens up the form? sample1.dotm |
#8
|
|||
|
|||
Part of your problem is you are deleting variables by setting them equal to ""
Demo: Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey With ActiveDocument .Variables("Test").Value = "Test" MsgBox "This document contains: " & .Variables.Count & " variable" 'Setting a name variable value to "" deletes the variable .Variables("Test").Value = "" On Error Resume Next MsgBox ActiveDocument.Variables("Test").Value MsgBox "This document contains: " & .Variables.Count & " variables" End With End Sub You need to have something in the variable even if it is just " " If you look at the code in Graham's example, you will see that is what he did Yes, provide other users allow macros to run in their documents, if you set up this way then it should work for everyone. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Hide/Unhide a Block of Text Based on Choice Selection | inquirer | Word VBA | 7 | 09-22-2014 04:41 PM |
Hide Checkbox When Printing | vinceplunkett | Word | 1 | 12-03-2013 01:53 AM |
Show/hide paragraphs based on upfront decisions | miscia76 | Word | 1 | 11-13-2013 04:29 PM |
VBA for CheckBox to Hide Slides | mutchy25 | PowerPoint | 1 | 09-21-2012 01:40 AM |
Macro to populate a text form field based on dropdown selection | koloa | Word | 0 | 10-20-2011 11:52 AM |