Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-14-2024, 08:49 AM
Shelley Lou Shelley Lou is offline VBA Adding missing Punctuation to end of Paragraphs Windows 10 VBA Adding missing Punctuation to end of Paragraphs Office 2016
Expert
VBA Adding missing Punctuation to end of Paragraphs
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Adding missing Punctuation to end of Paragraphs

Hi Andrew, so I've had a go at trying to update the code - the original code doesn't remove any spaces or non breaking spaces from the end of the paragraphs first, so when running the code its not inserting any punctuation at all - I've added some code to try and delete the space or non breaking space but I'm getting an error message at oRng.text = "" saying range cannot be deleted.



I'm also a bit stumped on how to change the comma or space before the string sLastWord to a semi colon - the string also needs to find instances of 'and/or' - in code terms would this be classed as 3 words or 6 characters?

If you could help at all I would be really grateful - thanks

Code:
Sub AddPuncDemo3()
  Dim Para As Paragraph, oRng As Range, sLastWord As String, sFirstChar As String
  'Application.ScreenUpdating = False
  For Each Para In ActiveDocument.Paragraphs
    With Para.Range
    
    Set oRng = Para.Range
            oRng.End = oRng.End - 1
            oRng.Collapse 0
            oRng.MoveStartWhile Chr(32), wdBackward 'Space
            oRng.MoveStartWhile Chr(160), wdBackward 'Non breaking space
            oRng.text = "" 'ERROR SAYING RANGE CANNOT BE DELETED
      
      If .Information(wdInFieldResult) Or .Information(wdWithInTable) Or .Font.AllCaps Or .Font.Bold Or Len(.text) < 3 Then
        GoTo NextFor
      Else
        Do While .Characters.Last.Previous = " "
          .Characters.Last.Previous.Delete
        Loop
        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", "and/or", "then", "plus", "minus", "less", "nor"
            'do nothing
            
              'ADD IN HERE TO CHANGE COMMA OR SPACE BEFORE THESE WORDS TO SEMI COLON IF ONE IS NOT ALREADY THERE
              
          Set oRng = .Words.Last.Previous.Words(1)
                        oRng.MoveStartWhile Chr(32), wdBackward 'Space
                        oRng.MoveStartWhile Chr(160), wdBackward 'Non breaking space
                        oRng.Start = oRng.Start - 1
                        If oRng.Characters(1) = ";" Then
                        'do nothing
                        If oRng.Characters(1) = "," Or " " Then
                        'change comma to semi colon or add semi colon if space
                        .Characters.Last.InsertBefore ";"
              
            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
Reply With Quote
Reply



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 02:23 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