#1
|
|||
|
|||
VBA to show/hide checkboxes- always showing bookmark
Hello-
I have a VBA code to show a bookmark based on the checkbox being selected, however, every time I load the word template all of the bookmarks under one section are showing unless I select/unselect them before I get started on filling out the document. The checkboxes are in a straight line across my word document. There are times where 1 option is selected and other times where all could be selected. My VBA code is Private Sub CheckBox6_Click() ActiveDocument.Bookmarks("LT").Range.Font.Hidden = Not CheckBox6.Value End Sub Private Sub CheckBox9_Click() ActiveDocument.Bookmarks("MT").Range.Font.Hidden = Not CheckBox9.Value End Sub Private Sub CheckBox10_Click() ActiveDocument.Bookmarks("HT").Range.Font.Hidden = Not CheckBox10.Value End Sub Private Sub CheckBox11_Click() ActiveDocument.Bookmarks("XHT").Range.Font.Hidden = Not CheckBox11.Value End Sub Private Sub CheckBox12_Click() ActiveDocument.Bookmarks("XHTT").Range.Font.Hidden = Not CheckBox12.Value End Sub Private Sub CheckBox13_Click() ActiveDocument.Bookmarks("PP").Range.Font.Hidden = Not CheckBox13.Value End Sub Private Sub CheckBox14_Click() ActiveDocument.Bookmarks("T").Range.Font.Hidden = Not CheckBox14.Value End Sub What am I doing wrong? |
#2
|
||||
|
||||
The code is only run when you click in a checkbox and it doesn't automatically execute at specific times (like at document open) because you haven't coded for that.
I suggest you set up the document differently so that the coding becomes far simpler. First, get rid of the old school checkboxes and replace them with Checkbox Content Controls. With each of these checkbox CCs, set the Title property to match the name of the bookmark you want to hide with it. Then paste this following code into your ThisDocument module. Code:
Private Sub Document_ContentControlOnExit(ByVal myContentControl As ContentControl, Cancel As Boolean) If myContentControl.Type = wdContentControlCheckBox Then Document_Open End Sub Private Sub Document_Open() Dim aCC As ContentControl For Each aCC In ActiveDocument.ContentControls If aCC.Type = wdContentControlCheckBox Then If ActiveDocument.Bookmarks.Exists(aCC.Title) Then ActiveDocument.Bookmarks(aCC.Title).Range.Font.Hidden = Not aCC.Checked End If End If Next aCC End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you!
That worked perfectly. One more question, previously I had it where a section would show/hide based on checkbox. Then at the bottom of the page where I had to document my pricing it would also show/hide there based on a bookmark with a different name. For example- Bookmark name Property would show a section for property data, then it would also show the bookmark named Propertypricing. How do I do that with the content control checkboxes?
|
#4
|
||||
|
||||
Hmm, bookmarks can only exist once in a document so you can't bookmark the second region with the same bookmark name. You could append a digit to the related bookmarks and adjust the code to also look for extra digits in the bookmark name.
Alternately, you could move away from the bookmark method of tagging areas for hiding and use other content controls for this purpose. The benefit of using Content Controls for your tagging is that you can loop through CCs which share an attribute like Tag or Title. For example Code:
Dim aCC As ContentControl For Each aCC In ActiveDocument.SelectContentControlsByTag("Hi") aCC.Range.Font.Hidden = True Next aCC
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Check boxes
Thank you once again for your time. I don't think I explained myself that well on my earlier reply.
So I had the checkboxes which I've gotten rid of and changed to content control check boxes like you suggested which is amazing. However, those original checkboxes had VBA code tied to them like this: Private Sub Auto_Click() ActiveDocument.Bookmarks("Auto").Range.Font.Hidden = Not AUTO.Value ActiveDocument.Bookmarks("BAHazard").Range.Font.Hi dden = Not AUTO.Value ActiveDocument.Bookmarks("BAUTUW").Range.Font.Hidd en = Not AUTO.Value ActiveDocument.Bookmarks("AutoPricing").Range.Font .Hidden = Not AUTO.Value ActiveDocument.Bookmarks("BALRS").Range.Font.Hidde n = Not AUTO.Value End Sub However, now with the content control checkbox and the VBA code that you were gracious enough to help me with I am only able to show/hide one bookmark vs the 5 that were tied to the previous *check boxes* |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
UserForm with checkboxes that hide/show bookmarked text in document | dohertym | Word VBA | 4 | 05-16-2022 09:48 PM |
Checkbox - toggle hide/unhide other checkboxes and their texts | Basse | Word VBA | 2 | 05-11-2022 10:43 PM |
Hide one bookmark when Two checkboxes are checked | AVarg123 | Word VBA | 4 | 04-01-2022 06:21 PM |
REf Fields show Bookmark whole cell when Bookmark is created by code. | pmcpowell | Word VBA | 2 | 11-16-2019 07:05 PM |
Bookmark will not show/hide based on CC Checkbox | lord_kaiser | Word VBA | 1 | 04-17-2018 01:19 AM |