Consulting

Results 1 to 3 of 3

Thread: How do I add a shape object to a Powerpoint macro that resizes itself?

  1. #1
    VBAX Newbie
    Joined
    Feb 2008
    Posts
    1
    Location

    How do I add a shape object to a Powerpoint macro that resizes itself?

    This is the code that I've been working with so far:
    --------------
    Sub AddTextBox()
    Dim MyText
    MyText = InputBox("Enter text here")
    For i = 1 To ActivePresentation.Slides.Count
        With ActivePresentation.Slides(i).Shapes.AddS 0, 0, 720, 30).TextFrame
            .TextRange.Text = MyText
       End With
    Next
    End Sub
    --------------
    I've been reading about the AutoSize method but can't seem to get it to work with my code. All I want to do is create a shape that reshapes itself to fit the text. Any help would be very, very much appreciated!
    Last edited by Aussiebear; 04-28-2023 at 08:37 PM. Reason: Adjusted the code tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    Maybe ...

    Sub AddTextBox()
        Dim oSlide As Slide
        Dim oShape As Shape
        Dim MyText As String
        Dim nWidth As Single
        MyText = InputBox("Enter text here")
    If Len(MyText) = 0 Then Exit Sub
        nWidth = ActivePresentation.SlideMaster.Width
        For Each oSlide In ActivePresentation.Slides
            With oSlide.Shapes.AddTextBox(msoTextOrientationHorizontal, 0, 0, nWidth, 30)
                .TextFrame.TextRange.Text = MyText
                .TextFrame.AutoSize = ppAutoSizeShapeToFitText
            End With
        Next
    End Sub

    Also, it's easier to read if you use the VBA tag insert box

    Paul
    Last edited by Aussiebear; 04-28-2023 at 08:39 PM. Reason: Adjusted the code tags

  3. #3
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Or maybe

    Sub AddTextBox()
    Dim MyText
    Dim oTxtbox As Shape
    Dim osld As Slide
    MyText = InputBox("Enter text here")
    For Each osld In ActivePresentation.Slides
        Set oTxtbox = osld.Shapes.AddTextBox(msoTextOrientationHorizontal, 10, 10, 20, 20)
        With oTxtbox
            .TextFrame.WordWrap = False
            .LockAspectRatio = False
            .TextFrame.TextRange = MyText
            .TextFrame.AutoSize = ppAutoSizeShapeToFitText
        End With
    Next osld
    End Sub
    Last edited by Aussiebear; 04-28-2023 at 08:38 PM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •