PDA

View Full Version : [SOLVED:] Seemingly random error with paragraph spacing



h2whoa
02-06-2018, 05:24 AM
So, I'm using a macro to align text boxes across multiple slides (one slide at a time, select text box to align with macro, click the assigned macro button, rinse and repeat).

Most of the time it works absolutely fine. But then on random slides (I can't work out the pattern), the paragraph spacing (.SpaceAfter) goes wrong. Instead of, in this particular example, a space of 3 pts between paragraphs, it will set it to 28.8 pts between paragraphs.

For the life of me, I cannot work out why this is happening. It would be less confusing if it happened all the time, but on most slides it works fine!

Any help is appreciated. Current version of the code I'm using is below.



Sub TextBoxAlign()If ActiveWindow.Selection.Type <> ppSelectionShapes Then Exit Sub
With ActiveWindow.Selection.ShapeRange(1)
.LockAspectRatio = False
.Height = cm2Points(0.6)
.Width = cm2Points(25.24)
.Left = cm2Points(0)
.Top = cm2Points(13.69)

With .TextFrame
.WordWrap = True
.VerticalAnchor = msoAnchorBottom
.AutoSize = ppAutoSizeNone

With .TextRange

With .Font
.Size = 8
.Name = "Arial(Body)"
.Bold = False
.Color.RGB = RGB(92, 92, 92)
End With

With .ParagraphFormat
.SpaceBefore = 0
.SpaceAfter = 3
.Alignment = ppAlignLeft
End With

End With
End With
End With
End Sub

Function cm2Points(inVal As Single)
cm2Points = inVal * 28.346
End Function

(I take no credit for any of this code, as it has been cobbled together from the hard work of others; I think it was originally John Wilson who developed the original code).

John Wilson
02-06-2018, 08:07 AM
.SpaceAfter / .SpaceBefore sets the value to xx LINES or xx POINTS

This is governed by setting .LineRuleAfter / .LineRuleBefore to True or False

Since you haven't set this it just depends on the current setting which may look fairly random.

If you are using a modern version I would also use new TextRange2 features and kill the error you will get if the selected shape does not have a textframe

This should get you started (again)


Sub TextBoxAlign()
On Error Resume Next
Dim oshp As Shape
Set oshp = ActiveWindow.Selection.ShapeRange(1)
If Not oshp Is Nothing And oshp.HasTextFrame Then
With oshp
.LockAspectRatio = False
.Height = cm2Points(0.6)
.Width = cm2Points(25.24)
.Left = cm2Points(0)
.Top = cm2Points(13.69)

With .TextFrame2
.WordWrap = True
.VerticalAnchor = msoAnchorBottom
.AutoSize = ppAutoSizeShapeToFitText
With .TextRange
With .Font
.Size = 8
.Name = "Arial(Body)"
.Bold = False
.Fill.ForeColor.RGB = RGB(92, 92, 92)
End With

With .ParagraphFormat
.LineRuleAfter = msoFalse
.SpaceBefore = 0
.SpaceAfter = 3
.Alignment = ppAlignLeft
End With

End With
End With
End With
End If ' something is wrong
End Sub

Function cm2Points(inVal As Single)
cm2Points = inVal * 28.346
End Function

h2whoa
02-06-2018, 08:37 AM
John, thank you so much for your help! Really appreciate you taking the time.