Link to home
Start Free TrialLog in
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Setting the SpaceAfter for a paragraph causes huge spacing with certain shapes in VBA

I am trying to set the SpaceAfter property of a paragraph of text in a shape within PowerPoint with this line of code in the Immediate Window of the VBE:

ActiveWindow.Selection.ShapeRange(1).TextFrame.TextRange.ParagraphFormat.SpaceAfter=6

Open in new window

Sometimes it works and sometimes it doesn't. When it doesn't work, it appears to set the spacing to a very large value, which looks like it's 6 * Font Size * 1.2 = 115.2pt in my example with 16pt text!!!

But even more odd is that when I read back the value with the following line, I get 6:

?ActiveWindow.Selection.ShapeRange(1).TextFrame.TextRange.ParagraphFormat.SpaceAfter

Open in new window

The attached deck demonstrates the issue. The left and right shapes have their spacing set to the incorrect value whilst the centre one doesn't.

What is happening?
Paragraph-Spacing-Comparison.pptx
ASKER CERTIFIED SOLUTION
Avatar of John Wilson
John Wilson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jamie Garroch (MVP)

ASKER

Thanks John. I examined all significant properties and found it to be LineRuleAfter (MSDN : "Determines whether line spacing after the last line in each paragraph is set to a specific number of points or lines")

Sub ExamineParagraphFormat()
  Dim oPF1 As ParagraphFormat
  Dim oPF2 As ParagraphFormat
  
  Set oPF1 = ActiveWindow.View.Slide.Shapes(1).TextFrame.TextRange.ParagraphFormat
  Set oPF2 = ActiveWindow.View.Slide.Shapes(2).TextFrame.TextRange.ParagraphFormat
  
  Debug.Print "Shape Name", oPF1.Parent.Parent.Parent.Name, oPF2.Parent.Parent.Parent.Name
  Debug.Print "Alignment", oPF1.Alignment, oPF2.Alignment
  Debug.Print "BaseLineAlignment", oPF1.BaseLineAlignment, oPF2.BaseLineAlignment
  Debug.Print "FarEastLineBreakControl", oPF1.FarEastLineBreakControl, oPF2.FarEastLineBreakControl
  Debug.Print "HangingPunctuation", oPF1.HangingPunctuation, oPF2.HangingPunctuation
  Debug.Print "LineRuleAfter", oPF1.LineRuleAfter, oPF2.LineRuleAfter
  Debug.Print "LineRuleBefore", oPF1.LineRuleBefore, oPF2.LineRuleBefore
  Debug.Print "LineRuleWithin", oPF1.LineRuleWithin, oPF2.LineRuleWithin
  Debug.Print "SpaceAfter", oPF1.SpaceAfter, oPF2.SpaceAfter
  Debug.Print "SpaceBefore", oPF1.SpaceBefore, oPF2.SpaceBefore
  Debug.Print "SpaceWithin", oPF1.SpaceWithin, oPF2.SpaceWithin
  Debug.Print "TextDirection", oPF1.TextDirection, oPF2.TextDirection
  Debug.Print "WordWrap", oPF1.WordWrap, oPF2.WordWrap
End Sub

Open in new window

Output for the two orange shapes:
Shape Name    Left (NOK)     Right (OK)
Alignment                    1             1 
BaseLineAlignment            1             5 
FarEastLineBreakControl     -1            -1 
HangingPunctuation          -1            -1 
LineRuleAfter               -1             0 
LineRuleBefore               0             0 
LineRuleWithin              -1            -1 
SpaceAfter                   0             0 
SpaceBefore                 10            10 
SpaceWithin                0.9           0.9 
TextDirection                1             1 
WordWrap                    -1            -1 

Open in new window