vbams-word

How to hide unchecked checkboxes when printing


I am working on creating a contract specification page in Word 2013 with checkboxes so that my boss can click each box that he wants to include in the final printed contract, and hide the ones he doesn't need. I'm completely new to VBA but I know that I need to use it to achieve this. From searching the internet I've used bookmarks and the code below:

Private Sub CheckBox1_Click()
 If CheckBox1.Value = False Then
 ActiveDocument.Bookmarks("Work1").Range.Font.Hidden = True
 Else
 ActiveDocument.Bookmarks("Work1").Range.Font.Hidden = False
 End If
 End Sub

But this code seems to hide the checkboxes before I can click them. I would like the checkboxes to stay visible until they print, in case my boss needs to make a change. I also tried using another code, but it also didn't work the way I wanted it to:

Private Sub CheckBox1_Click()
  If CheckBox1.Value = False Then
    ActiveDocument.Bookmarks("Work1").Application.Options.PrintHiddenText = False
Else
    ActiveDocument.Bookmarks("Work1").Application.Options.PrintHiddenText = True
End If
End Sub

I would also like to make it so there are no gaps where the unused checkboxes would be. Any help would be greatly appreciated!!


Solution

  • I seem to have overcomplicated the issue yesterday. Your only problem was that your checkboxes turn unvisible along with the text. You just need to bookmark exactly the text you want to hide/show leaving the checkbox (perceived by Word as a part of the text) outside of the bookmark.

    Your code will do this fine, or you can replace it with this:

    Private Sub CheckBox1_Click()
    Bookmarks("Work1").Range.Font.Hidden = Not CheckBox1
    End Sub
    

    By the way, if you make Word show paragraph marks (Ctrl + *), you will be able to see hidden text.