#1
|
|||
|
|||
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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA help with punctuation code | Shelley Lou | Word VBA | 0 | 12-09-2022 05:39 AM |
Adding punctuation to body of text | Sweetpea7 | Word | 5 | 08-29-2021 07:44 PM |
punctuation order | fariz | Word | 2 | 10-31-2016 12:57 AM |
Punctuation | lexsper | Word | 0 | 04-06-2015 07:26 AM |
Paragraph border missing on interior paragraphs | strathglass | Word | 1 | 09-19-2011 02:41 PM |