PDA

View Full Version : [SOLVED:] All margins 0 for shapes and tables from one button



RayKay
01-22-2019, 03:33 AM
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 :help, 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

John Wilson
01-22-2019, 04:31 AM
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

RayKay
01-22-2019, 05:33 AM
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

John Wilson
01-22-2019, 09:00 AM
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

RayKay
01-22-2019, 09:17 AM
That's great, thanks John. I made it work :)
Superb! Have a great day, thank you :thumb