Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2014, 10:35 AM
tammytran105 tammytran105 is offline Show/Hide Text based on Checkbox Selection Windows XP Show/Hide Text based on Checkbox Selection Office XP
Novice
Show/Hide Text based on Checkbox Selection
 
Join Date: Sep 2014
Posts: 3
tammytran105 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 09-29-2014, 08:48 PM
macropod's Avatar
macropod macropod is offline Show/Hide Text based on Checkbox Selection Windows 7 64bit Show/Hide Text based on Checkbox Selection Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #3  
Old 09-29-2014, 08:50 PM
gmayor's Avatar
gmayor gmayor is offline Show/Hide Text based on Checkbox Selection Windows 7 64bit Show/Hide Text based on Checkbox Selection Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 09-30-2014, 02:20 PM
tammytran105 tammytran105 is offline Show/Hide Text based on Checkbox Selection Windows XP Show/Hide Text based on Checkbox Selection Office XP
Novice
Show/Hide Text based on Checkbox Selection
 
Join Date: Sep 2014
Posts: 3
tammytran105 is on a distinguished road
Default

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!!
Reply With Quote
  #5  
Old 09-30-2014, 09:27 PM
macropod's Avatar
macropod macropod is offline Show/Hide Text based on Checkbox Selection Windows 7 64bit Show/Hide Text based on Checkbox Selection Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 tammytran105 View Post
I tried doing the DocVariable Field thing from Graham's link, but I wasn't able to get it to work.
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]
Reply With Quote
  #6  
Old 09-30-2014, 09:41 PM
gmayor's Avatar
gmayor gmayor is offline Show/Hide Text based on Checkbox Selection Windows 7 64bit Show/Hide Text based on Checkbox Selection Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

See the attached which uses doc variables at their simplest.
Attached Files
File Type: docm CheckBoxes.docm (29.4 KB, 508 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 10-02-2014, 04:03 PM
tammytran105 tammytran105 is offline Show/Hide Text based on Checkbox Selection Windows XP Show/Hide Text based on Checkbox Selection Office XP
Novice
Show/Hide Text based on Checkbox Selection
 
Join Date: Sep 2014
Posts: 3
tammytran105 is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 10-02-2014, 04:30 PM
gmaxey gmaxey is offline Show/Hide Text based on Checkbox Selection Windows 7 32bit Show/Hide Text based on Checkbox Selection Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Show/Hide Text based on Checkbox Selection Hide/Unhide a Block of Text Based on Choice Selection inquirer Word VBA 7 09-22-2014 04:41 PM
Show/Hide Text based on Checkbox Selection Hide Checkbox When Printing vinceplunkett Word 1 12-03-2013 01:53 AM
Show/Hide Text based on Checkbox Selection Show/hide paragraphs based on upfront decisions miscia76 Word 1 11-13-2013 04:29 PM
Show/Hide Text based on Checkbox Selection 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:18 AM.


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