Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 02-15-2024, 12:22 AM
Guessed's Avatar
Guessed Guessed is offline VBA Adding missing Punctuation to end of Paragraphs Windows 10 VBA Adding missing Punctuation to end of Paragraphs Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

OK, try this variation for those conditions
Code:
Sub AddPuncDemo3()
  Dim Para As Paragraph, oRng As Range, sLastWord As String, sFirstChar As String
  'Application.ScreenUpdating = False
  With ActiveDocument.Range.Find
    .ClearFormatting
    .Text = "^w^p"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
  End With
  
  For Each Para In ActiveDocument.Paragraphs
    With Para.Range
      If .Information(wdInFieldResult) Or .Information(wdWithInTable) Or .Font.AllCaps Or .Font.Bold Or Len(.Text) < 3 Then
        GoTo NextFor
      Else
        sFirstChar = .Characters(1)
        sLastWord = .Words.Last.Previous.Words(1)
        Debug.Print sFirstChar, sLastWord
        If Not sLastWord Like "*[.!?:;]" Then 'If para ends with any of these characters do nothing
          Select Case sLastWord
            Case "and", "but", "or", "then", "plus", "minus", "less", "nor"
              Set oRng = .Words.Last    '.Previous.Words(1)
              oRng.MoveStartUntil cSet:=" ", Count:=-10
              Set oRng = oRng.Characters.First.Previous.Previous
              oRng.Select
              If oRng.Text = "," Then
                oRng.Text = ";"
              ElseIf oRng.Text Like "[a-z0-9]*" Then
                oRng.Collapse Direction:=wdCollapseEnd
                oRng.Text = ";"
              End If
            Case Else
              If sFirstChar = UCase(sFirstChar) Then
                .Characters.Last.InsertBefore "." 'Insert period if para starts Uppercase
              Else
                .Characters.Last.InsertBefore ";" 'Insert semi colon if para starts lowercase
              End If
          End Select
        End If
      End If
    End With
NextFor:
  Next
  'Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
 



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
VBA Adding missing Punctuation to end of Paragraphs punctuation order fariz Word 2 10-31-2016 12:57 AM
Punctuation lexsper Word 0 04-06-2015 07:26 AM
VBA Adding missing Punctuation to end of Paragraphs Paragraph border missing on interior paragraphs strathglass Word 1 09-19-2011 02:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft