Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 06-17-2021, 09:20 AM
Shelley Lou Shelley Lou is offline VBA code help: replacing punctuation Windows 10 VBA code help: replacing punctuation Office 2016
Expert
VBA code help: replacing punctuation
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA code help: replacing punctuation


Hi, I would like to tidy up this code.
1. After each sentence the code runs to remove punctuation and replace with a semi-colon. It doesn't seem to work unless I put the .Text = ".^p" bit of the code to remove periods first though. Is there a better way to do this?
2. The code needs to remove punctuation before a square bracket at the end of sentences only. I have put the code in as .Text = ".]^p" etc. which removes punctuation, what I need is for the code that inserts the semi-colon, to search for square brackets and insert the semi-colon before the square bracket not after it.

I'm really not sure how to make this happen, can anyone help please.

Code:
 'Remove period from end of sentence
    Set orng = ActiveDocument.Range
    With orng.Find
    .Text = ".^p"
   .Replacement.Text = "^p"
   .Execute Replace:=wdReplaceAll
    End With
    'Remove period before square bracket at end of sentence
       Set orng = ActiveDocument.Range
    With orng.Find
      .Text = ".]^p"
      .Replacement.Text = "]^p"
      .Execute Replace:=wdReplaceAll
      End With
      'Insert semi-colon at end of sentences but not for and, but, or, then
      Set orng = ActiveDocument.Range
    For Each Para In ActiveDocument.Paragraphs
    With Para.Range
      If Len(.Text) > 2 Then
        If Not .Characters.Last.Previous Like "[.!?:;]" Then
          Select Case .Words.Last.Previous.Words(1)
            Case "and", "but", "or", "then"
              'do nothing
            Case Else
              .Characters.Last.InsertBefore ";"
          End Select
        End If
      End If
    End With
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code help: replacing punctuation How to delete all the punctuation marks from a paragraph? Jamal NUMAN Word 3 02-26-2019 02:33 PM
VBA code help: replacing punctuation punctuation order fariz Word 2 10-31-2016 12:57 AM
VBA code help: replacing punctuation Need help with modifying a replacing font VBA code- similar task but subtle change kissingfrogs2003 Word VBA 3 08-30-2016 11:42 AM
Replacing punctuation marks with footnotes nufc89 Word 2 05-22-2015 03:15 PM
Punctuation lexsper Word 0 04-06-2015 07:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:19 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