Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-02-2023, 02:42 PM
KSmart KSmart is offline VBA to show/hide checkboxes- always showing bookmark Windows 10 VBA to show/hide checkboxes- always showing bookmark Office 2021
Novice
VBA to show/hide checkboxes- always showing bookmark
 
Join Date: Nov 2023
Posts: 3
KSmart is on a distinguished road
Default 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?
Reply With Quote
  #2  
Old 11-03-2023, 04:39 PM
Guessed's Avatar
Guessed Guessed is offline VBA to show/hide checkboxes- always showing bookmark Windows 10 VBA to show/hide checkboxes- always showing bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 11-06-2023, 04:28 PM
KSmart KSmart is offline VBA to show/hide checkboxes- always showing bookmark Windows 10 VBA to show/hide checkboxes- always showing bookmark Office 2021
Novice
VBA to show/hide checkboxes- always showing bookmark
 
Join Date: Nov 2023
Posts: 3
KSmart is on a distinguished road
Default 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?
Reply With Quote
  #4  
Old 11-06-2023, 04:56 PM
Guessed's Avatar
Guessed Guessed is offline VBA to show/hide checkboxes- always showing bookmark Windows 10 VBA to show/hide checkboxes- always showing bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 11-07-2023, 02:24 PM
KSmart KSmart is offline VBA to show/hide checkboxes- always showing bookmark Windows 10 VBA to show/hide checkboxes- always showing bookmark Office 2021
Novice
VBA to show/hide checkboxes- always showing bookmark
 
Join Date: Nov 2023
Posts: 3
KSmart is on a distinguished road
Default 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*
Reply With Quote
Reply



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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:35 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