0
votes

I would like to hide the text that's is not highlighted in my document.

I've already code the part where I can't do it on "normal" text, but I got textboxes with highlighted text inside and I cant access to them.

I've try to do some research to get inside this text boxes but nothing works.

Here is my code :

Sub Hide_onCondition_RSO()
'
' Hide_RSO Macro
'
'
    Dim eShape As Word.shape
    Dim i As Long
    
    For i = ActiveDocument.Shapes.Count To 1 Step -1
    Set eShape = ActiveDocument.Shapes(i)
    If eShape.Type = msoTextBox Then
         With ActiveDocument.Content.Find
        .Text = ""
        .ClearFormatting
        .Format = True
        .Highlight = False
        .Replacement.Text = ""
        .Replacement.ClearFormatting
        .Replacement.Font.Hidden = True
        .Execute Replace:=wdReplaceAll
    End With
    End If
    Next i
End Sub

Do you have some ideas ?

------------------------------ EDIT -----------------------------

When I try to loop around shapes, and try to access to the text frame of the shape to see if the text is highlighted or no, that the macro doesn't find them because maybe they are in a group (?).

enter image description here

1

1 Answers

2
votes

You need to execute the find on each textbox.

Sub Hide_onCondition_RSO()
    '
    ' Hide_RSO Macro
    '
    '
    Dim eShape As Word.Shape
    
    For Each eShape In ActiveDocument.Shapes
        If eShape.Type = msoTextBox Then
            With eShape.TextFrame.TextRange.Find
                .Text = ""
                .ClearFormatting
                .Format = True
                .Highlight = False
                .Replacement.Text = ""
                .Replacement.ClearFormatting
                .Replacement.Font.Hidden = True
                .Execute Replace:=wdReplaceAll
            End With
        End If
    Next eShape
End Sub

EDIT:

To find textboxes in a grouped shape you need to loop through the shape's GroupItems collection. For example:

Sub Hide_onCondition_RSO()
    Dim shp As Word.Shape
    Dim subshp As Word.Shape
    
    For Each shp In ActiveDocument.Shapes
        Select Case shp.Type
        Case msoTextBox
            FindInTextBox shp
        Case msoGroup
            For Each subshp In shp.GroupItems
                If subshp.Type = msoTextBox Then FindInTextBox subshp
            Next subshp
        End Select
    Next shp
End Sub

Sub FindInTextBox(txtbox As Word.Shape)
    With txtbox.TextFrame.TextRange.Find
        .Text = ""
        .ClearFormatting
        .Format = True
        .Highlight = False
        .Replacement.Text = ""
        .Replacement.ClearFormatting
        .Replacement.Font.Hidden = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub