Log in

View Full Version : Automatically deleting final (empty) paragraph from textboxes



GertVC
11-07-2013, 08:47 AM
Hello,

i've created a macro in excel that creates slides in PowerPoint using a predefined slide and the data in excel.
In some of my text boxes, the last (bullet-ed) paragraph only contains a space and i would like to delete this paragraph
The code below is code i found online and which i adapted (tried to adapt) to suit my needs.



Sub ClearPara(ActivePresentation As Object)
' Replace matching paragraphs with nothing
Dim sld As Object, shp As Object, intCount As Integer, para As Object, strTarget As String
strTarget = Chr(32)
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.TextRange.Paragraphs.Count > 0 Then
' More than one paragraph; loop backwards
For intCount = shp.TextFrame.TextRange.Paragraphs.Count _
To 1 Step -1
Set para = shp.TextFrame.TextRange.Paragraphs(intCount)
If intCount = shp.TextFrame.TextRange.Paragraphs.Count Then
' No trailing CR+LF to match; leaves a blank bullet
'MsgBox (para.Text)
If para.Text = strTarget Then
para.ParagraphFormat.Bullet.Type = ppBulletNone
para.Delete

End If
Else
If para.Text = strTarget & Chr(13) & Chr(10) Then
para.Delete
End If
End If
Next
End If
End If
Next
Next
End Sub


the command
para.deletedoes remove any text that is present, but it doesn't remove the paragraph brake. Because of this an empty bullet and paragraph brake remain present in the text box.

Any ideas on how the remove this page break?

any help is immensely appreciated
Best regards
Gert

John Wilson
11-07-2013, 10:38 AM
Maybe

Sub ClearPara()
Dim sld As Object, shp As Object
Dim otxR As Object
Const SPACE As String = " "
strTarget = Chr(32)
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
Set otxR = shp.TextFrame.TextRange
While otxR.Characters(otxR.Characters.Count) = SPACE _
Or otxR.Characters(otxR.Characters.Count) = vbCr
otxR.Characters(otxR.Characters.Count).Delete
Wend
End If
End If
Next shp
Next sld
End Sub

GertVC
11-07-2013, 11:57 AM
That did exactly what i wanted it to do.
Thank you so much!

John Wilson
11-08-2013, 01:15 AM
Yep

I had this exact problem with an AddIn I wrote for a Multi National Bank so had the code already done!

It's good when normal people can BENEFIT from the big banks!