PDA

View Full Version : Change sentence case



epimeliad
06-23-2016, 04:49 AM
Hi all,

Desperately need help with trying to replace all text except in full caps and title to be proper sentence case.

I tried using ppCaseSentence but it also changes some of my full cap acronyms.

So far i tried following Steve Rindsberg code but I failed to make it work



Sub DataScrubAllSlidesAndTables()
Dim sld As Slide
Dim grpItem As Shape
Dim shp As Shape
Dim i As Long
Dim j As Long


For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes


If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.ChangeCase ppCaseSentence
End If
End If


If shp.HasTable Then
For i = 1 To shp.Table.Rows.Count
For j = 1 To shp.Table.Columns.Count
shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.ChangeCase ppCaseSentence
Next j
Next i
End If


Next shp
Next
End Sub

John Wilson
06-23-2016, 07:39 AM
Don't think there is any built in way to check whether text is ALREADY TitleCase or ALL CAPS.

Paul_Hossler
06-23-2016, 11:06 AM
You could pass the .TextRanges to a function something like this and put them back





Option Explicit
Sub test()
Dim sBefore As String, sAfter As String

sBefore = "now is the time for FBI and For the NASA to come to the aid. NoW is the time for USA and For the UK to"

MsgBox sBefore
sAfter = SentenceCase(sBefore)
MsgBox sAfter

End Sub

Function SentenceCase(sIn As String) As String
Dim a As Variant
Dim i As Long

a = Split(sIn, " ")

'if first word not all UC, capitialize first letter
If a(LBound(a)) <> UCase(a(LBound(a))) Then
a(LBound(a)) = UCase(Left(a(LBound(a)), 1)) & LCase(Right(a(LBound(a)), Len(a(LBound(a))) - 1))
End If
'rest LC unless already all in UC
For i = LBound(a) + 1 To UBound(a)
If a(i) <> UCase(a(i)) Then
a(i) = LCase(a(i))
End If
Next

'check if multiple sentences by looking to see if previous ends in . ? ! or "
For i = LBound(a) + 1 To UBound(a)
If InStr(".?!""", Right(a(i - 1), 1)) > 0 Then
If a(i) <> UCase(a(i)) Then
a(i) = UCase(Left(a(i), 1)) & LCase(Right(a(i), Len(a(i)) - 1))
End If
End If
Next
SentenceCase = Join(a, " ")
End Function