Log in

View Full Version : Hard spaces; Macro resets text formatting



Rosenrot
01-06-2017, 05:20 PM
Hi everyone,

The aim of the code below was to insert hard spaces before certain words so that they automatically jump into the next line. The problem is that when a shape contains some words in bold, the VBA code turns off this particular formatting.

Do you have any idea how to improve the code so that it preserves the text formatting? Thank you in advance!

Sub NextLine()
Dim oSld As Slide
Dim oShp As Shape

With ActivePresentation
For Each oSld In .Slides
For Each oShp In oSld.Shapes

If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Text = Replace(oShp.TextFrame.TextRange.Text, "and" & Chr$(32), "and" & Chr$(160))
oShp.TextFrame.TextRange.Text = Replace(oShp.TextFrame.TextRange.Text, "or" & Chr$(32), "or" & Chr$(160))

End If
End If
Next
Next
End With
End Sub

John Wilson
01-07-2017, 03:57 AM
Does that code even work?

Maybe something like this would do what you need:


Sub addHard()
Dim osld As Slide
Dim oshp As Shape
Dim TRtemp As TextRange
Dim TR As TextRange
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.HasTextFrame Then
If oshp.TextFrame.HasText Then
Set TR = oshp.TextFrame.TextRange
Set TRtemp = TR.Replace("and ", "and" & vbCrLf)
Set TRtemp = TR.Replace("or ", "or" & vbCrLf)
Do While Not TRtemp Is Nothing
Set TRtemp = TR.Replace("and ", "and" & vbCrLf)
Set TRtemp = TR.Replace("or ", "or" & vbCrLf)
Loop
End If
End If
Next oshp
Next osld
End Sub

Rosenrot
01-09-2017, 04:01 AM
That was helpful, thank you John!