View Single Post
 
Old 05-30-2021, 12:26 AM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

The two macros work on different principles so it is more efficient to do this in different ways for the two macros. Adding the brackets is quite slow because it steps through every paragraph in the document and you wouldn't want to do that 3 times. Removing them is much faster as the search is a more efficient way of finding a style so it can afford to repeat the process three times.
Code:
Sub DamnBrackets()
  Dim aPar As Paragraph, aRng As Range, sty As String
  For Each aPar In ActiveDocument.Paragraphs
    sty = aPar.Style
    If sty = "Normal" Or sty = "AbNormal" Or sty = "Heading 1" Then
      Set aRng = aPar.Range
      Do While Left(aRng.Text, 1) = " "
        aRng.MoveStart Unit:=wdCharacter, Count:=1
      Loop
      Do While Right(aRng.Text, 1) = vbCr Or Right(aRng.Text, 1) = " "
        aRng.MoveEnd Unit:=wdCharacter, Count:=-1
      Loop
      If Len(aRng.Text) > 0 Then
        aRng.InsertAfter ")"
        aRng.InsertBefore "("
      End If
    End If
  Next aPar
End Sub

Sub RemoveBrackets()
  Dim arrSty() As String, i As Integer
  arrSty = Split("Normal|AbNormal|Heading 1", "|")
  For i = LBound(arrSty) To UBound(arrSty)
    With Selection.Find
      .ClearFormatting
      .Style = ActiveDocument.Styles(arrSty(i))
      .Text = "[\(\)]{1}"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .MatchWildcards = True
      .Format = True
      .Execute Replace:=wdReplaceAll
    End With
  Next i
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote