Consulting

Results 1 to 6 of 6

Thread: Setting Internal Margins for a Textbox in PowerPoint Macro

  1. #1

    Angry Setting Internal Margins for a Textbox in PowerPoint Macro

    Hi, below is my code. I am creating a textbox macro that allows you to insert a textbox with footnotes at the bottom of the page for my company's standard powerpoint slide deck. First line says "Note:" and second line says "Source". I want to format the textbox so that the internal margins on the top, bottom, left and right are all "0". Can you please help?? I am not a very technical person and have gotten this far but am now stuck! Thanks!

    Sub addtxtbox()
    Dim osld As Slide
    Dim oSh As Shape
    For Each osld In ActiveWindow.Selection.SlideRange
       Set oSh = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=13, _
       Top:=500, Width:=659, Height:=16)
       With oSh.TextFrame.TextRange
          ' MUST add text first
          ' else PPT ignores subsequent text formatting
          .Font.Name = "Tahoma"
          .Font.Size = 6
          .ParagraphFormat.Alignment = ppAlignLeft
          'Set the text
          .Text = "Note:" & vbCrLf & "Source:"
          ' or even
          ' .Text = cstr(oSld.SlideIndex)
       End With
    Next osld
    End Sub
    Last edited by Aussiebear; 04-12-2023 at 06:45 PM. Reason: Adjusted the code tags

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Sub addtxtbox()Dim osld As Slide
    Dim oSh As Shape
    For Each osld In ActiveWindow.Selection.SlideRange
       Set oSh = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=13, _
       Top:=500, Width:=659, Height:=16)
       With oSh.TextFrame
          .MarginBottom = 0
          .MarginLeft = 0
          .MarginTop = 0
          .MarginRight = 0
       End With
       With oSh.TextFrame.TextRange
          ' MUST add text first
          ' else PPT ignores subsequent text formatting
          .Font.Name = "Tahoma"
          .Font.Size = 6
          .ParagraphFormat.Alignment = ppAlignLeft
          'Set the text
          .Text = "Note:" & vbCrLf & "Source:"
          ' or even
          ' .Text = cstr(oSld.SlideIndex)
       End With
    Next osld
    End Sub
    Last edited by Aussiebear; 04-12-2023 at 06:47 PM. Reason: Adjusted the code tags
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Thank you John, and bravo, this is so cool. In my case, I would like to select one or more objects in PowerPoint (these could be text boxes or shapes that hold text) and use a macro to apply zero margins to the selected objects. Different than the OP, I would like to apply the margins to selected objects rather than add new ones. By way of background I am a total newbie and I have been laughing at my attempt at trying to leverage your code over my morning coffee for my own application and it seems just beyond my reach. I am excited to make it work because I have some other ideas for little macros like this that would make my life a lot easier. I know it's a tall order to ask a stranger over the internet — would you be able to please provide a solution?

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Maybe this

    Sub noMargins()
    
    Dim oshp As Shape
    For Each oshp In ActiveWindow.Selection.ShapeRange
    If oshp.HasTextFrame Then
    With oshp.TextFrame
    .MarginBottom = 0
    .MarginLeft = 0
    .MarginTop = 0
    .MarginRight = 0
    End With
    End If
    Next
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location
    Hi! This is another version, which John helped me with last year, which I'm eternally grateful for.

    It removes all margins in shapes AND table cell(s), whatever's selected:

    HTML Code:
    Sub noMargins()
     
    Dim I As Integer
        Dim otbl As Table
        Dim iRow As Integer
        Dim iCol As Integer
        On Error Resume Next
        err.Clear
        Set otbl = Application.ActiveWindow.Selection.ShapeRange(1).Table
        If Not otbl Is Nothing Then
        For iRow = 1 To otbl.Rows.Count
        For iCol = 1 To otbl.Columns.Count
        If otbl.Cell(iRow, iCol).Selected Then
          
    With otbl.Cell(iRow, iCol).Shape.TextFrame2
        .MarginBottom = 0
        .MarginLeft = 0
        .MarginRight = 0
        .MarginTop = 0
    End With
     
        End If    ' selected
        Next iCol
        Next iRow
        End If    ' Table selected
     
    Dim L As Long
        Dim oshp As Shape
        Set oshp = ActiveWindow.Selection.ShapeRange(1)
       
    If oshp.HasTextFrame Then
    If oshp.TextFrame2.HasText Then
    With oshp.TextFrame2.TextRange
    For L = 1 To .Paragraphs.Count
     
    Next L
    End With
     
    With ActiveWindow.Selection.ShapeRange
    .TextFrame.MarginLeft = 0
    .TextFrame.MarginRight = 0
    .TextFrame.MarginTop = 0
    .TextFrame.MarginBottom = 0
    End With
     
    End If
    End If
     
    End Sub

  6. #6
    Thank you John and RayKay! John, that was exactly what I was looking for. Now I just have to figure out how to pin it to my toolbar so I always have access to it and I will be all set! I am on a Mac and it does not look like it will be easy, but I will figure it out.

Tags for this Thread

Posting Permissions

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