Share via

Macro To Hide Blue Text

Anonymous
2020-05-22T17:27:51+00:00

I am working on a document and want to hide text that is blue. I have the below macro that hides the text if I create it as a bookmark, but since the document is large I wondered if there was a way to hide based on the font color? 

Any help will be appreciated!

Private Sub CheckBox1_Click()

If ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = True Then

ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = False

Else: ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = True

End If

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2020-05-22T19:42:36+00:00

    The following macro uses the color that is listed as Blue in the dropdown color palette. If you use a different color blue, you'll have to change the number 12611584 in the code.

    Private Sub CheckBox1_Click()

        With ActiveDocument.Content.Find

            .ClearFormatting

            .Font.Color = 12611584

            .Replacement.ClearFormatting

            .Replacement.Font.Hidden = True

            .Text = ""

            .Replacement.Text = ""

            .Format = True

            .Execute Replace:=wdReplaceAll

        End With

    End Sub

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. HansV 462.6K Reputation points
    2020-05-23T16:36:04+00:00

    " This appears to work to hide the text. How would I write it to unhide if the checkbox is not checked? "

    Like this:

    Private Sub CheckBox1_Click()

        With ActiveDocument.Content.Find

            .ClearFormatting

            .Font.Color = 12611584

            .Replacement.ClearFormatting

            .Replacement.Font.Hidden = Me.CheckBox1

            .Text = ""

            .Replacement.Text = ""

            .Format = True

            .Execute Replace:=wdReplaceAll

        End With

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2020-05-23T10:57:35+00:00

    The fact the range to process might be large, doesn't detract from the ability to process it via a bookmark.

    FWIW, your code could be reduced to:

    Private Sub CheckBox1_Click()

    ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = Me.CheckBox1.Value

    End Sub

    or:

    Private Sub CheckBox1_Click()

    ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = Not Me.CheckBox1.Value

    End Sub

    or even:

    Private Sub CheckBox1_Click()

    With ActiveDocument.Bookmarks("bookmark").Range.Font

      .Hidden = .Hidden

    End With

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Charles Kenyon 167.4K Reputation points Volunteer Moderator
    2020-05-23T01:46:17+00:00

    I think Hans has answered your question.

    Your problem would be better addressed by using styles for your formatting. Then the solution would be as easy as modifying a style.

    Importance of Styles in Word

    If you are not making use of styles, you are shooting yourself in the foot, making your work much harder than it needs to be.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  5. Anonymous
    2020-05-23T12:58:36+00:00

    Thanks Hans! This appears to work to hide the text. How would I write it to unhide if the checkbox is not checked?

    Was this answer helpful?

    0 comments No comments