Log in

View Full Version : Solved: .HasTextRange seems to skip some objects



Paul_Hossler
08-13-2011, 02:02 PM
Doing simple macro to set all text to Arial and Black

Works ... mostly ... but .HasTextRange seems to miss some shapes, e.g. Rectangles

What do I need to add to this??


Option Explicit

Sub FormatAllTextboxes()

Dim oPres AsPresentation
Dim oSlide AsSlide
Dim oShape AsShape

Set oPres =ActivePresentation

For Each oSlide InoPres.Slides
For Each oShape In oSlide.Shapes
If oShape.HasTextFrame Then
With oShape.TextFrame.TextRange
If Len(.Text) > 0 Then
.Font.Name = "Arial"
.Font.Color = 0
Else
oShape.Delete
End If
End With
End If
Next
Next
End Sub


Paul

John Wilson
08-13-2011, 11:46 PM
Hi Paul

Deleting shapes within a loop that has a count of the shapes often confuses PowerPoint and skips shapes or even causes a crash.

Best practise is to loop backwards through the shapes

Sub FormatAllTextboxes()

Dim oPres As Presentation
Dim oSlide As Slide
Dim oShape As Shape
Dim ICOUNT As Integer
Set oPres = ActivePresentation

For Each oSlide In oPres.Slides
If oSlide.Shapes.Count > 0 Then
For ICOUNT = oSlide.Shapes.Count To 1 Step -1
Set oShape = oSlide.Shapes(ICOUNT)
If oShape.HasTextFrame Then

With oShape.TextFrame.TextRange
If Len(.Text) > 0 Then
.Font.Name = "Arial"
.Font.Color = 0
Else
oShape.Delete
End If
End With
End If
Next ICOUNT
End If
Next oSlide
End Sub

LOGIC

Suppose you have five shapes and in your loop and delete shape 3
Now there are four shapes and shapes(4) will become Shapes(3) and Shapes(5) >> Shapes(4)
When the loop gets to the next shape it's Shapes(3)
HANG ON I just deleted shapes(3) NOW I'M CONFUSED maybe I'll just ignore this one!

Paul_Hossler
08-14-2011, 06:44 AM
Oh, ^&*!!#$%%$$&)(&^%%*$&%$^%^ :censored:

How embarrassing

I KNEW that

The .Delete was just an after (not very) thought.

I had about 150 slides in some one else's presentation, and threw the .Delete in at the last minute when I noticed empty shapes.

I focused on the re-format part, and COMPLETELY missed the effects of the .Delete.

Thanks !!

Paul

John Wilson
08-14-2011, 02:02 PM
Catches lots of folk out. It's not obvious what the problem is till someone explains it!