View Single Post
 
Old 02-12-2024, 02:20 PM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Expert
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Adding missing Punctuation to end of Paragraphs

I'm trying to write a macro which adds punctuation if it is missing. I want the macro to add a period to the end of all paragraphs that start with a Capital Letter or a semi colon if paragraphs start lowercase. These paragraphs may also contain a comment at the end.

If the lowercase paragraphs end with any of these words "and/or", "and", "but", "or" a semi-colon needs to be inserted before these words replacing any punctuation already there e.g. a comma.

I'm getting myself in a bit of a muddle with the Select Case and End Select throwing up errors and I can't work out why.

Would appreciate any advice on where I am going wrong. Thanks.


Punctuation.docx

Code:
Sub AddPuncDemo1()
Application.ScreenUpdating = False
Dim Para As Paragraph, oRng As Range
On Error Resume Next
For Each Para In ActiveDocument.Paragraphs
  With Para.Range
    If .Characters.Last.Previous.InRange(ActiveDocument.TablesOfContents(1).Range) = False Then 'Not to include table of contents
    If Para.Range.Information(wdWithInTable) = False Then 'Not to include if in a table
            If Len(.text) > 2 And Not .Font.Bold And Not .Font.AllCaps Then 'Not to incude if para is bold or ALL UPPERCASE
          If Not .Characters.Last.Previous Like "[.!?:;]" Then 'If para ends with any of these characters do nothing
          Select Case .Words.Last.Previous.Words(1)
            Case "and/or", "and", "but", "or", "then", "plus", "minus", "less", "nor" 'If para ends with any of these words do nothing
            Case Else 'do nothing
          If .Characters(1).Case = wdUpperCase = True Then
          .Characters.Last.InsertBefore "." 'Insert period if para starts with capital letter
          If .Characters(1).Case = wdLowerCase = True Then
          .Characters.Last.InsertBefore ";" 'Insert semi colon if para starts lowercase
       
         Select Case .Words.Last.Previous.Words(1)
                    Case "and/or", "and", "but", "or", "then", "plus", "minus", "less", "nor"
                        Set oRng = .Words.Last.Previous.Words(1)
                        oRng.MoveStartWhile Chr(32), wdBackward
                        oRng.MoveStartWhile Chr(160), wdBackward
                        oRng.Start = oRng.Start - 1
                        If oRng.Characters(1) = ";" Then
                        'if semi-colon before case words do nothing else
                        End If
                        If oRng.Characters(1) = "," Then
                        'if have comma before case words convert comma to semi-colon
                        .Characters.Last.InsertBefore ";"
                        End If
                    Case Else
                End Select
            End If
      End If
      End If
      End If
      End If
      End If
  End With
Next
Application.ScreenUpdating = True
End Sub
Reply With Quote