Macro to remove brackets in Word

inarbeth

Well-known Member
Joined
Apr 8, 2002
Messages
913
I wonder if any VBA experts can help - I appreciate this applies to MS Word. I found the following macro to remove square brackets.

Sub RemoveSquareBrackets()

Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "\[*\]"
.MatchWildcards = True
While .Execute
With oRng
.Select
Select Case MsgBox("Do you want to remove brackets?", vbQuestion + vbYesNoCancel, "???")
Case Is = vbYes
.Characters.First.Delete
.Characters.Last.Delete
.Collapse wdCollapseEnd
Case Is = vbNo
.Collapse wdCollapseEnd
Case Else
Exit Sub
End Select
End With
Wend
End With
lbl_Exit:
Exit Sub
End Sub

I want a simpler macro that simply removes the first brackets to the left and right of the cursor (i.e. just one pair of brackets) and does not cycle through the document. If there are no brackets or only a single bracket then a pop up to give a warning.
Many thanks in advance.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I want a simpler macro that simply removes the first brackets to the left and right of the cursor (i.e. just one pair of brackets) and does not cycle through the document
Why do you think that should be simpler? :p

Not very proud of the code, but this macro seems do the job:
VBA Code:
Sub KillBracks()
'Vedi https://www.mrexcel.com/board/threads/macro-to-remove-brackets-in-word.1252904/
'
Dim aaB As Long, aaF As Long, ooB As Long, ooE As Long
Dim bbO As Long, bbC As Long
Dim cSStr As String, oSStr As String
'
ooB = Selection.Start                                   'B & E of original selection
ooE = Selection.End
oSStr = Selection.Range.Text
Selection.Collapse Direction:=wdCollapseStart
Selection.Find.Execute FindText:="[", Forward:=False    'Search [
aaB = Selection.Start                                   'its adr
Selection.End = ooE
Selection.Collapse Direction:=wdCollapseEnd
Selection.Find.Execute FindText:="]", Forward:=True      'Search ]
aaF = Selection.Start                                    '  its adr

bbO = CountString("[", aaB + 1, aaF - 1)                 'Count [ in calcullated range
bbC = CountString("]", aaB + 1, aaF - 1)                 'Count ] in calcullated range
Selection.Start = aaB - 0                                'Select calculated range
Selection.End = aaF + 1
cSStr = Selection.Range.Text                             'Get its Text
If bbO = 0 And bbC = 0 And aaF > aaB Then                'Checks
    Selection.Range.Characters.First.Text = vbNullString 'If OK
    Selection.End = aaF
    Selection.Range.Characters.Last.Text = vbNullString
    MsgBox ("Found: " & cSStr & " and Cleaned")          '?????
    Debug.Print "COMPLETED"                              'Debug Info
    Debug.Print "O++:" & oSStr
    Debug.Print "C++:" & cSStr
Else                                                     'If Wrong
    MsgBox ("Found: " & cSStr & vbCrLf _
      & "The found text is incorrectly formed; operation ABORTED")
    Debug.Print "FAILED", bbO + bbC, aaF, aaB            'Debug Info
    Debug.Print "O__:" & oSStr
    Debug.Print "C__:" & cSStr
End If
End Sub
If you don't need the confirmation message (but I suggest you keep it) then remove the line marked ?????

Try...
my test file: D:\DDownloads\Questa è una.docm
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top