Anything you do to show/hide content by toggling the font properties is unreliable in a corporate environment. That's because whether text formatted as hidden actually displays or prints has little to do with that property. Rather, it all depends on how the user has Word configured. Depending on that configuration, text formatted as hidden may:
• neither display nor print
• display but not print
• print but not display
• both display and print
Although it's probably safe to programmatically turn off the hidden-text printing, users may not appreciate you fiddling with its display, since toggling it off also toggles off the display of other formatting, such as spaces, tabs & paragraph breaks.
If you really want to control what happens, you need to conditionally insert/delete the content. To do that, try:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
With ContentControl
Select Case .Title
Case "CheckBox1"
If .Checked = True Then
Call UpdateBookmark("BkMk1", "Conditional text")
Else
Call UpdateBookmark("BkMk1", "")
End If
Case "CheckBox2"
If .Checked = True Then
Call UpdateBookmark("BkMk2", "Conditional text")
Else
Call UpdateBookmark("BkMk2", "")
End If
Case Else
End Select
End With
Application.ScreenUpdating = True
End Sub
Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With ActiveDocument
If .Bookmarks.Exists(StrBkMk) Then
Set BkMkRng = .Bookmarks(StrBkMk).Range
BkMkRng.Text = StrTxt
.Bookmarks.Add StrBkMk, BkMkRng
End If
End With
Set BkMkRng = Nothing
End Sub
There are two issues with this approach:
1. Bookmarks are all too easily deleted by ham-fisted users, breaking the process; and
2. Doing anything more than simple text output requires considerably more coding.
An alternative approach, which would allow the content to remain in the document would be to use Custom Document Properties and field coding. This is easier for users to maintain and simplifies anything you might want to do that entails formatting:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
With ContentControl
Select Case .Title
Case "CheckBox1"
If .Checked = True Then
ActiveDocument.CustomDocumentProperties("Chk1").Value = 1
Else
ActiveDocument.CustomDocumentProperties("Chk1").Value = 0
End If
Case "CheckBox2"
If .Checked = True Then
ActiveDocument.CustomDocumentProperties("Chk2").Value = 1
Else
ActiveDocument.CustomDocumentProperties("Chk2").Value = 0
End If
Case Else
End Select
End With
Application.ActiveWindow.View.ShowFieldCodes = False
Application.Options.PrintFieldCodes = False
ActiveDocument.Fields.Update
Application.ScreenUpdating = True
End Sub
With this approach, you don't need any bookmarks and the same Custom Document Property can be used to update multiple locations in the document - all you need is a field coded along the lines of the following for each:
{IF{DOCPROPERTY Chk1}= 1 "True Text"} or:
{IF{DOCPROPERTY Chk1}= 1 "True Text" "False Text"}
where field brace pairs (i.e. '{ }') are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. The output can contain pictures and tables as well as text. You create the Custom Document Properties via File|Properties|Advanced Properties>Custom. With this approach, the content remains in the document and is readily accessible by pressing Alt-F9 to reveal the field contents. It's still possible to show & print the field codes instead of their values but, since few users want to do either, the macro can fairly safely turn both of those options off.
Bookmarks