PDA

View Full Version : [SOLVED:] Setting Internal Margins for a Textbox in PowerPoint Macro



msftmonkey1
03-23-2018, 04:56 PM
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

John Wilson
03-24-2018, 12:02 AM
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

PPTHelper
08-11-2019, 08:07 AM
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?

John Wilson
08-12-2019, 06:45 AM
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

RayKay
08-12-2019, 07:54 AM
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:



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

PPTHelper
08-12-2019, 01:43 PM
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.