0

I'm trying to create a form where a user can tick a box and paragraphs of text are displayed

I've used the code below, however when I click out of design mode the text disappears (as expected) but when I click the check box it doesn't reappear

Private Sub CHECKbutane_Click()
If (Bookmarks("TEXT_Butane").Range.Font.Hidden = True) Then
Bookmarks("TEXT_Butane").Range.Font.Hidden = False

Else

Bookmarks("TEXT_Butane").Range.Font.Hidden = True

End If
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You could try using a Selection.Find and finding .Font.Hidden = true then on find, Replacement.Font.Hidden = false. You will have to make sure your .Format in Selection.Find is true and your Selection.Find.Text = "" – interesting-name-here Apr 14 '16 at 17:31
  • So, this checkbox is an ActiveX control on the surface of the document? If you put a MsgBox in the code or a Debug.Print does that execute when the box is clicked? Don't you want to hide/unide the text according to the setting of the checkbox, rather than just toggling? – Cindy Meister Apr 14 '16 at 18:03

2 Answers2

1

When you work with using the Hidden property to hide/show text, make sure that the display of Hidden text is turned off in the Word UI and the display of all non-printing options must also be turned off. The individual optinos are in File/Options/Display; all non-printing characters can be toggled on/off using the "backwards P" in the Home tab.

Of course, if this is a macro to be used by others, no one wants to have to continually go into File/Options/Display to change these settings. Here's a macro that turns on the individual settings for everything except hidden text if the non-printing characters are being displayed.

The display of non-printing characters if then turned off and the display of hidden text is turned on/off according to the state of the checkbox.

Private Sub CheckBox1_Click()
    Dim vw As Word.View
    Dim bChecked As Boolean
    Dim bkm As Word.Bookmark

    'If the user is currently viewing non-printing characters
    'make sure these are turned on individually so that
    'not displaying Hidden text does not affect these settings.
    Set vw = Application.ActiveWindow.View
    If vw.ShowAll = True Then
        vw.ShowParagraphs = True
        vw.ShowObjectAnchors = True
        vw.ShowTabs = True
        vw.ShowHyphens = True
        vw.ShowOptionalBreaks = True
        vw.ShowSpaces = True
    End If
    vw.ShowAll = False
    vw.ShowHiddenText = False

    bChecked = Me.CheckBox1.Value
    Set bkm = ActiveDocument.Bookmarks("TEXT_Butane")
    If bChecked Then
        bkm.Range.Font.Hidden = False
    Else
        bkm.Range.Font.Hidden = True
    End If
End Sub

If this were a professional application, you'd want to store the individual "Show" settings and re-apply them when this document is no longer the active document. But that's very advanced programming...

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
0

I got the following code to work for me.

So you could essentially:

Public Hide As Boolean

Sub CHECKbutane_Click()

    Bookmarks("TEXT_Butane").Range.Select

    If Hide Then

        Hide = False
        Call Hide

    Else

        Hide = True
        Call Unhide

    End If
End Sub

Sub Unhide()

    With Selection.find

        .text = ""
        .Format = True
        .Font.Hidden = True
        .Replacement.Font.Hidden = False
        .MatchWildcards = False

    End With

    Do While Selection.find.Execute

        Selection.Font.Hidden = False
        Selection.MoveRight 1

    Loop

End Sub

Sub Hide()

    With Selection.find

        .text = ""
        .Format = True
        .Font.Hidden = False
        .Replacement.Font.Hidden = True
        .MatchWildcards = False

    End With

    Do While Selection.find.Execute

        Selection.Font.Hidden = True
        Selection.MoveRight 1

    Loop

End Sub
interesting-name-here
  • 1,851
  • 1
  • 20
  • 33