Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mulitple Checkboxes (Word VBA)

Status
Not open for further replies.

Flunked

Technical User
Oct 5, 2009
2
GB
Hi Guys,

I am new to VBA and have managed to get this far using books and help files on the internet. I would like to know if it is possible to amend the code to allow this to work on multiple checkboxes, if it is possible could somebody please show me how? i.e. if i click on checkbox1 it shows the text, if i click on checkbox 2 it shows the text. I managed to amend a little but when I click on any checkbox it shows all the text and not just the text for the checbox that was ticked.


Code:
Sub CheckBox1_Change()
    Call ShowHideBookmark
End Sub
 
 
Sub ShowHideBookmark()
    Dim orange As Range
    Set orange = ActiveDocument.Bookmarks("Bookmarkname").Range
    If CheckBox1.Value = True Then
        With orange.Font
            .Hidden = True
        End With
        With ActiveWindow.View
            .ShowHiddenText = False
            .ShowAll = False
        End With
    Else
        With orange.Font
            .Hidden = False
        End With
        With ActiveWindow.View
            .ShowHiddenText = True
            .ShowAll = False
    
        End With
    End If
    
End Sub
 
I am confused.

" if i click on checkbox1 it shows the text, if i click on checkbox 2 it shows the text."

Does that mean it does not matter if you click checkbox 1 OR checkbox 2 - they both display the same text?

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Hi Fumei

I want each checkbox just to display just the text for that particular question on the word document. i.e. if I have 10 questions, I would have 10 checkboxs and ten answers.

If I clicked on checkbox5 I just want it to display the answer to question5. I am very new to VBA and don't know how to amend the above code to allow this...
 
The easiest way is to put each specific change into each specific _Change procedure.

Checkbox1_Change = change to text associated with it.
Checkbox2_Change = change to text associated with it.
Checkbox3_Change = change to text associated with it.
etc.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Hi Flunked,

A potential problem you'll have with your approach is that the hidden text will still be visible if the user resets the hidden text visibility via Tools|Options|View and, perhaps moreimportantly, the hidden text will print if the 'hidden text' option is checked under Tools|Options|Print.

A far more secure approach is to use code to insert into or delete from the bookmark the conditional text, according to the checkbox state.


Cheers
[MS MVP - Word]
 
I agree.

Also, you are cross-posting this question at VBAExpress.

macropod, I pointed out the same thing to the OP on the other thread, and their response was:
I am aware people can view hidden text if they edit the options. However the people who initially read this document said they was to much text, hence why I want to hide the answers. No chance of them altering any settings

Shrug. What can I say? Good point about the Print issue though. I would agree that it is perhaps more important than the Tools > Option visibility.

Just in case you do not quite understand what macropod is suggesting...

Instead of the checkbox state (True/False) changing the Font.Hidden state of the bookmark (which as has been pointed out has issues with visibility and printing), the checkbox state (True/False) would change the actual content of the bookmarks.

It could get a little messy depending on the quantity of text going in/out, but technically speaking it does not really matter. There are a few different ways to handle the text.

I have posted a new demo file at VBAExpress that uses a checkbox. Please note that THIS thread asks about using checkboxes, but the VBAExpress thread asks the same thing about "buttons". Both threads are asking to do the same thing.

It is considered impolite to cross-post and NOT mention it.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Hi Flunked,

No chance of them altering any settings
In the real world, changing settings is exactly what happens and, even if the users don't change any settings, some or all of them might already having systems set to print hidden text.


Cheers
[MS MVP - Word]
 
Good point. Relying on users to NOT do something is almost asking for trouble.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top