Consulting

Results 1 to 5 of 5

Thread: All margins 0 for shapes and tables from one button

  1. #1
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location

    All margins 0 for shapes and tables from one button

    Hi John, I know you're very busy and I'm very grateful for your time in the past. I've spent a few hours trying to solve this.

    I have the below code, putting two working codes together from VBA Express (great site btw!) and my efforts fail

    It's to make all 4 margins in Tables AND Shapes to be 0, 0, 0, 0.

    The code works separately on selected Table Cells and Selected Shapes, but I was hoping to put them into one tool.

    Thanks in advance , thank you.



    Code:

    Sub ZeroMargins()


    With ActiveWindow.Selection.ShapeRange
    .TextFrame.MarginLeft = 0
    .TextFrame.MarginRight = 0
    .TextFrame.MarginTop = 0
    .TextFrame.MarginBottom = 0
    End With


    Dim TBLShape As Table
    Dim TBLCell As Cell
    Dim iCell As Integer
    Set TBLShape = ActiveWindow.Selection.ShapeRange.Table
    For iCell = 1 To TBLShape.Rows.Count
    For Each TBLCell In TBLShape.Rows(iCell).Cells
    With TBLCell.Shape.TextFrame
    .MarginTop = 0
    .MarginLeft = 0
    .MarginRight = 0
    .MarginBottom = 0
    End With
    Next
    Next
    Set TBLCell = Nothing
    Set TBLShape = Nothing


    End Sub

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    This is the OUTLINE for what you need to do. You will need to add the relevant code for tables and shapes and declare the variables for the table

    Sub ZeroMargins()
    Dim oshp As Shape
    Dim osld As Slide
    Dim otbl As Table
    For Each osld In ActivePresentation.Slides
    For Each oshp In osld.Shapes
    If oshp.HasTable Then
    Set otbl = oshp.Table
    With otbl
    ' put the table code here
    End With
    Else
    If oshp.HasTextFrame Then
    With oshp.TextFrame
    ' code for shapes goes here
    End With
    End If
    End If
    Next oshp
    Next osld
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location
    Hi thanks John, but I'm having difficulty. VBA in PowerPoint keeps showing errors when Debugging. I've tried inserting different table and shapes code lines. Could you kindly show me? Thank you.

    My amateur code:

    Sub ZeroMargins()


    Dim oshp As Shape
    Dim osld As Slide
    Dim otbl As Table
    Dim TBLCell As Cell
    Dim iCell As Integer


    For Each osld In ActivePresentation.Slides
    For Each oshp In osld.Shapes
    If oshp.HasTable Then
    Set otbl = oshp.Table
    With otbl
    ' put the table code here


    Set otbl = ActiveWindow.Selection.ShapeRange.Table
    For iCell = 1 To otbl.Rows.Count
    For Each TBLCell In otbl.Rows(iCell).Cells
    With TBLCell.Shape.TextFrame
    .MarginTop = 0
    .MarginLeft = 0
    .MarginRight = 0
    .MarginBottom = 0
    End With
    Next
    Next
    Set TBLCell = Nothing
    Set otbl = Nothing


    End With
    Else
    If oshp.HasTextFrame Then
    With oshp.TextFrame


    ' code for shapes goes here
    With ActiveWindow.Selection.ShapeRange
    .TextFrame.MarginLeft = 0
    .TextFrame.MarginRight = 0
    .TextFrame.MarginTop = 0
    .TextFrame.MarginBottom = 0


    End With
    End If
    End If
    Next oshp
    Next osld
    End Sub

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Sub ZeroMargins()
    Dim oshp As Shape
    Dim osld As Slide
    Dim otbl As Table
    Dim TBLCell As Cell
    Dim iCell As Integer
    For Each osld In ActivePresentation.Slides
    For Each oshp In osld.Shapes
    If oshp.HasTable Then
    Set otbl = oshp.Table
    With otbl
    ' put the table code here
    
    
    'YOU HAVE ALREADY SET oTBL AND IT IS NOT THE SELECTION
    'SO DELETE THE NEXT LINE
    Set otbl = ActiveWindow.Selection.ShapeRange.Table 'DELETE
    For iCell = 1 To otbl.Rows.Count
    For Each TBLCell In otbl.Rows(iCell).Cells
    With TBLCell.Shape.TextFrame
    .MarginTop = 0
    .MarginLeft = 0
    .MarginRight = 0
    .MarginBottom = 0
    End With
    Next
    Next
    Set TBLCell = Nothing
    Set otbl = Nothing
    End With
    Else
    If oshp.HasTextFrame Then
    With oshp.TextFrame
    ' code for shapes goes here
    'AGAIN YOU ARE NOT LOOKING AT THE SELECTION
    'NEXT LINE TO BE DELETED
    With ActiveWindow.Selection.ShapeRange 'DELETE
    .MarginLeft = 0
    .MarginRight = 0
    .MarginTop = 0
    .MarginBottom = 0
    End With
    End If
    End If
    Next oshp
    Next osld
    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
    That's great, thanks John. I made it work
    Superb! Have a great day, thank you

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
  •