Search for text box formatted to Layout: In line with text

D

Dan B

Hi.

I've managed to set up a macro that hides the content of all text boxes in
my document by searching for each text box and setting the contents' font
colour to white. However, it only seems to find text boxes that haven't been
formatted to 'Layout: In line with text'.

Does anybody know way to search for and edit these text boxes as well as the
others?

Here is the code that I am using:

Sub HideAnswers()

' Find each text box and set it's font colour to white

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes

If aShape.Type = msoTextBox Then

aShape.Select
Selection.Font.Color = wdColorWhite

End If

Next

End Sub

Any help would be really appreciated.

Cheers,

Dan.
 
S

Stefan Blom

I'm guessing the problem is caused by your attempt to select the text. Use
this code instead:

Sub HideAnswers()

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes

If aShape.Type = msoTextBox Then

'Set the font color directly for each text box shape:
aShape.TextFrame.ContainingRange.Font.Color = wdColorWhite

End If

Next

End Sub


--
Stefan Blom
Microsoft Word MVP


in message
news:[email protected]...
 
J

Jean-Guy Marcil

Dan B said:
Hi.

I've managed to set up a macro that hides the content of all text boxes in
my document by searching for each text box and setting the contents' font
colour to white. However, it only seems to find text boxes that haven't been
formatted to 'Layout: In line with text'.

Does anybody know way to search for and edit these text boxes as well as the
others?

Here is the code that I am using:

Sub HideAnswers()

' Find each text box and set it's font colour to white

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes

If aShape.Type = msoTextBox Then

aShape.Select
Selection.Font.Color = wdColorWhite

End If

Next

End Sub

Normally, inLine shapes and floating shapes are two different collections.
You would have to iterate both of them. But, textboxes are different, even
when inline they retain their "Shape" status. However, your usage of the
Selection object is causing you grief. Only use the Selection object when you
must (i.e. there is no other way of achieveing a result).

Try this variation of your code:

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes
If aShape.Type = msoTextBox Then
With aShape
If .TextFrame.HasText Then
.TextFrame.TextRange.Font.Color = wdColorWhite
End If
End With
End If
Next
 
J

Jean-Guy Marcil

Stefan Blom said:
I'm guessing the problem is caused by your attempt to select the text. Use
this code instead:

Sub HideAnswers()

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes

If aShape.Type = msoTextBox Then

'Set the font color directly for each text box shape:
aShape.TextFrame.ContainingRange.Font.Color = wdColorWhite

End If

Next

End Sub
Just in case DanB (or other readers) does not know the difference between
our two suggestions I thougth I'd clarify a small technical point...
You used the .ContainingRange property which refers to the entire range of
text inside linked textboxes. I use the .TextRange property which refers to
the individual range of text inside each textbox, whether they are linked or
not. In this case, the only difference in the application is that if there
are, let's say, 3 linked textboxes, the code with the .ContainingRange
Property will apply the white font colour three times to the text inside the
linked textboxes. So, in this case, it does not matter which property one
uses, but with some other scenario, it might make a big difference!
 
S

Stefan Blom

Of course, generally it makes more sense to use .TextRange, since it returns
the range of a *specific* shape (excluding the ranges of any linked
shapes/text boxes). Thank you for adding this clarification.

--
Stefan Blom
Microsoft Word MVP


in message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top