Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-27-2021, 06:11 AM
Matt C's Avatar
Matt C Matt C is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 97-2003
Advanced Beginner
Enclose All Instances of a Specific Custom Style in Brackets
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Question Enclose All Instances of a Specific Custom Style in Brackets

Hi folks



I've been tinkering with the VBA code below which encloses curly brackets (parentheses) around selected text but am looking for a way to apply this method automatically to every paragraph in a document which is formatted with a specific custom style (let's call it "Custom Style").

Secondly, I then want to do the reverse (which can be a separate macro), i.e. remove left and right curly brackets from the beginning and end of "Custom Style" paragraphs).


Quote:
Dim iCount As Integer

iCount = 1

While Right(Selection.Text, 1) = " " Or _
Right(Selection.Text, 1) = Chr(13)
Selection.MoveLeft Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
iCount = iCount + 1

Wend

Selection.InsertAfter ")"
Selection.InsertBefore "("
Selection.MoveRight Unit:=wdCharacter, Count:=iCount
All help appreciated in advance. Many thanks.
Reply With Quote
  #2  
Old 05-27-2021, 08:36 PM
Guessed's Avatar
Guessed Guessed is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

Stage one macro might work along these lines
Code:
Sub DamnBrackets()
  Dim aPar As Paragraph, aRng As Range
  For Each aPar In ActiveDocument.Paragraphs
    If aPar.Style = "Normal" 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
To reverse it the simplest thing to do would be to find and replace those characters with nothing. If you need to identify which ones were inserted by the first macro then it becomes a lot harder unless you tagged them in some way as you insert them. Can you use brackets that don't appear elsewhere in the text?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 05-28-2021, 03:52 AM
Matt C's Avatar
Matt C Matt C is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 97-2003
Advanced Beginner
Enclose All Instances of a Specific Custom Style in Brackets
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Default

Thanks, Andrew, that worked a treat.

I'm finding the second part a bit tricky. I've got as far as the code below which only removes the first instance of a "(" from the relevant paragraph style, then stops. I couldn't get a standard find/replace to work - it just left all the brackets in place.

Quote:
Sub RemoveBrackets()

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Custom Style")
With Selection.Find
.Text = "("
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
Can I tweak the above to loop through the document finding and deleting both "(" and ")" at the same time (in only "Custom Style" paragraphs)?
Reply With Quote
  #4  
Old 05-28-2021, 04:38 AM
Matt C's Avatar
Matt C Matt C is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 97-2003
Advanced Beginner
Enclose All Instances of a Specific Custom Style in Brackets
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Default

UPDATE:

Sorry, I made some obvious minor errors with the above code. Here's my own tweaking which now works. However, it's a bit clunky as it repeats identical blocks of code to remove the "(" and ")" characters. Is there any way of slimming it down?

Quote:
Sub RemoveBracketsUPDATED()

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Custom Style")
With Selection.Find
.Text = "("
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Custom Style")
With Selection.Find
.Text = ")"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub
Reply With Quote
  #5  
Old 05-28-2021, 03:22 PM
Guessed's Avatar
Guessed Guessed is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

This gets rid of EVERY bracket in that style - including ones that the earlier macro didn't put in.
Code:
Sub RemoveBracketsUPDATED()
  With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Normal")
    .Text = "[\(\)]{1}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Format = True
    .Execute Replace:=wdReplaceAll
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 05-29-2021, 05:54 AM
Matt C's Avatar
Matt C Matt C is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 97-2003
Advanced Beginner
Enclose All Instances of a Specific Custom Style in Brackets
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Default

Thanks, Andrew. I've got this all working great now.

For reference, how would I adapt the above code to remove every bracket from, say, three specific custom styles (rather than just one)? I tried a few methods but couldn't get anything to work. I might need to do this in the future.
Reply With Quote
  #7  
Old 05-30-2021, 12:26 AM
Guessed's Avatar
Guessed Guessed is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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
  #8  
Old 05-30-2021, 04:52 AM
Matt C's Avatar
Matt C Matt C is offline Enclose All Instances of a Specific Custom Style in Brackets Windows 10 Enclose All Instances of a Specific Custom Style in Brackets Office 97-2003
Advanced Beginner
Enclose All Instances of a Specific Custom Style in Brackets
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Default

That's great, Andrew, thanks.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enclose All Instances of a Specific Custom Style in Brackets find and delet all text within brackets and the brackets themselves wrdy Word 2 08-03-2017 06:55 PM
Enclose All Instances of a Specific Custom Style in Brackets Word forces use of a specific style C_Hawk1996 Word 1 04-13-2016 05:35 AM
Enclose All Instances of a Specific Custom Style in Brackets How to set the left indent in a specific style. OfficeBoy95 Word 2 05-12-2014 06:31 PM
How do I add a custom style to tab button? kennethc Word 1 04-11-2014 05:25 PM
How to set style automatically for specific texts ragesz Word 2 07-25-2013 07:08 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:26 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft