Search code examples
vbapowerpoint

VBA code to set on all powerpoint textboxes to the option "Shrink text on overflow"


I am searching for a VBA code that activate the option "Shrink text on overflow" for all textboxes in a PowerPoint document. I tried this :

Sub Change() 
  Dim oSlide As Slide 
  Dim oShape As Shape 
  For Each oSlide In ActivePresentation.Slides
    For Each oShape In oSlide.Shapes 
      oShape.TextFrame2.AutoSize = MsoAutoSize.msoAutoSizeTextToFitShape    
    Next oShape  
  Next oSlide   
End Sub

But that unfortunately doesn't work. I am a very beginner.
Any thought ?
Thank you.


Solution

  • Just use msoAutoSizeTextToFitShape instead of MsoAutoSize.msoAutoSizeTextToFitShape. It's also good to check if the shape actually has a textframe first. This version gets shapes inside groups as well:

    Sub Change()
        Dim oSlide As Slide
        Dim oShape As Shape
        Dim oSubShape As Shape
        
        For Each oSlide In ActivePresentation.Slides
            For Each oShape In oSlide.Shapes
                If oShape.Type = msoGroup Then
                    For Each oSubShape In oShape.GroupItems
                        If oSubShape.HasTextFrame Then
                            oSubShape.TextFrame2.AutoSize = msoAutoSizeTextToFitShape
                        End If
                    Next oSubShape
                Else
                    If oShape.HasTextFrame Then
                        oShape.TextFrame2.AutoSize = msoAutoSizeTextToFitShape
                    End If
                End If
            Next oShape
        Next oSlide
    End Sub