Hello,

I have a special question. I have a word document which contains a lot of brackets. And In the brackets, there are some abbreviation of name, such as (A S P). Now I need to delete the brackets and the spaces in side , change it like this: ASP

I just can' find a way to batch do that automaticlly . Any idea? I'm using word2010

Dani AI

Generated

Quick add: and were right — Word wildcards or a small macro is the clean way. One small correction to ’s post: Kayla_2 used round parentheses ( ), not square brackets. The macro below automates exactly what you described: it finds parenthesized sequences made of single letters separated by spaces (for example "(A S P)"), removes the spaces and the parentheses, and leaves ordinary parenthetical phrases (like "(New York)") untouched.

Sub RemoveSpacesInsideParentheses_InitialsOnly()
  Dim fRng As Range, inner As String, re As Object
  Set re = CreateObject("VBScript.RegExp")
  re.Pattern = "^[A-Za-z]( +[A-Za-z])*$"   ' single-letter tokens only (A S P or A)
  re.IgnoreCase = True

  Set fRng = ActiveDocument.Content
  With fRng.Find
    .Text = "\([!)]@\)"
    .MatchWildcards = True
    .Wrap = wdFindStop
  End With

  Do While fRng.Find.Execute
    inner = Mid(fRng.Text, 2, Len(fRng.Text) - 2)  ' strip ( )
    If re.Test(inner) Then
      fRng.Text = Replace(inner, " ", "")           ' ASP
    End If
    fRng.Collapse wdCollapseEnd
  Loop
End Sub

How to use: press Alt+F11, Insert → Module, paste the code, close the editor, then run the macro (Alt+F8). To operate on a selection only, change Set fRng = ActiveDocument.Content to Set fRng = Selection.Range before running. Always test on a copy and use Undo if needed.

Notes/troubleshooting: this purposely only targets parentheticals made of single-letter tokens separated by spaces so common phrases won’t be collapsed. If your data contains periods (e.g. "A. S. P.") or mixed punctuation, tell me which exact variants you have and I’ll suggest a tiny pattern tweak.

Recommended Answers

All 3 Replies

Word supports regular expression operations that will do this. RTFM.

Hello,

For you case, you can Find and Replace or VBA code. Here are the 2 ways:

M1:
1.Enter (*) in “Find what” text box and Check “Use wildcards” box, to find all square brackets

  1. Clear “Find what” text box and enter [() ] , Click “Find In” and this time choose “Current Selection”.

  2. Press Delete

M2:
Run this VBAcode

Sub DeleteDelimiters(objFind As Find, strLeftDelimiter As String, strRightDelimiter As String, bDeleteSpace As Boolean)
  Dim strFind1 As String
  Dim strFind2 As String

  strFind1 = "\" & strLeftDelimiter & "*\" & strRightDelimiter
  If (bDeleteSpace) Then
    strFind2 = "[" & strLeftDelimiter & "\" & strRightDelimiter & " ]"
  Else
    strFind2 = "[" & strLeftDelimiter & "\" & strRightDelimiter & "]"
  End If

  Selection.HomeKey Unit:=wdStory
  objFind.ClearFormatting
  objFind.Replacement.Text = ""

  While objFind.Execute(FindText:=strFind1, MatchWildcards:=True)
    objFind.Execute FindText:=strFind2, MatchWildcards:=True, _
      ReplaceWith:="", Replace:=wdReplaceAll, Wrap:=wdFindStop
  Wend

End Sub

Sub DeleteBracketsAndSpace()
  Application.ScreenUpdating = False

  '  Delet all parenthesis and the space in it.
  Call DeleteDelimiters(Selection.Find, "(", ")", True)

  Application.ScreenUpdating = True
End Sub

Thanks, all, It really helps a lot. I think I have got my solution :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.