Log in

View Full Version : [SOLVED:] VBA to change all shapes to "Do not autofit"



PeterW
04-30-2019, 05:06 AM
Hello,

First post - first question. I hope someone here can help me. I've got a fairly large PowerPoint document, and would like to change all shapes' Text Options to "Do not autofit". The document contains grouped items and normal (not grouped) shapes. I sort of understand how code works, but am by no means a programmer. In the past, I've been able to modify VBA code I found online to suit my needs. This is what I've done so far, but I get error messages when running the script:



Sub ActivePresentation_DoNotAutofit()
Dim oshp As Shape
Dim osld As Slide
Dim L As Long
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.Type = msoGroup Then
For L = oshp.GroupItems.Count To 1 Step -1
Call DoNotAutofit(oshp.GroupItems(L))
Next L
Else
Call DoNotAutofit(oshp)
End If
Next oshp
Next osld
End Sub


Sub DoNotAutofit(oshp As Shape)
oshp.TextFrame2.Autosize = msoAutoSizeNone
End Sub


There's probably more elegant solutions for this, for example to implement to 2nd part into the main script. But like this, I can easily modify the script for other things. (And it makes things more simple when I look at the code again in a few months).
Can someone please help me?

Many thanks!
Peter

John Wilson
04-30-2019, 05:48 AM
Your code will error if it hits a shape that does not have a textframe.

Try:


Sub DoNotAutofit(oshp As Shape)
If oshp.HasTextFrame Then
oshp.TextFrame2.AutoSize = msoAutoSizeNone
End If
End Sub

PeterW
04-30-2019, 08:08 AM
This works great.
Thanks very much, John!