PDA

View Full Version : Brackets / space



Snowflake
10-14-2012, 06:26 AM
How to add space before and after brackets in the sentence?


Example 1: He was kind(at least he thought that)and good hearted.
Solution 1: He was kind (at least he thought that) and good hearted.

Many many thanks! :hi:

Paul_Hossler
10-14-2012, 07:50 AM
The easiest way is to use the space bar

The brute force way is to use find and replace (in a macro) to just repace a "(" with a " (" but if there's aready a space there, you'd get 2.

There's an implied requirement that says if there's more that one space " (", you only want one. (???)


Example 2: He was kind . . . . . ( . . . . . . at least he thought that . . . . . ) . . . .. and good hearted.

Soulution 2: He was kind (at least he thought that) and good hearted.

Correct?

There's probably a super-duper 'do it all at once' way that it could be done with regular expression, etc. but IMHO the most understandable / maintainable way is with multiple passes through the data. Trade off is performance.


So this just

1. Replaces " ( " with "( " and then that with "("
2. Replaces "(" with " ("
3. etc.



Sub Macro2()
With ActiveDocument.Content.Find
'replace n-spaces + ( with just (
.ClearFormatting
.Replacement.ClearFormatting
.Text = " {1,}\("
.Replacement.Text = "("
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll

'replace ( + n-spaces with just (
.Text = "\( {1,}"
.Execute Replace:=wdReplaceAll

'replace ( with 1-space + (
.ClearFormatting
.Replacement.ClearFormatting
.Text = "("
.Replacement.Text = " ("
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
'replace n-spaces + ) with just (
.ClearFormatting
.Replacement.ClearFormatting
.Text = " {1,}\)"
.Replacement.Text = ")"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll

'replace ) + n-spaces with just )
.Text = "\) {1,}"
.Execute Replace:=wdReplaceAll

'replace ) with ) + 1-space
.ClearFormatting
.Replacement.ClearFormatting
.Text = ")"
.Replacement.Text = ") "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub



Just my style

Paul

Snowflake
10-14-2012, 10:09 AM
That is a great way. I thought of just adding space after brackets if there isn't one and doing nothing if there is.
Something like that, but you did very good. Thank you!

Paul_Hossler
10-14-2012, 04:33 PM
BTW, you could probalbly make it work for quotation marks also.

I'd think about creating 2 subs just to keep the code maintainable

Paul

gmaxey
10-15-2012, 04:11 AM
Paul, I would like to see that super-duper method ;-). I tried just a "duper" and it came up short. It seems that Find falls over if you try to find "(" or ")" using wildcards "\(" "\)" and then put either in a group "(\()" or "(\))"

To get around that, I had to substitute ( and ) with ohter characters and then restore them. Anyway, this code does something similiar to your, but looks at a few other things which may or may not be important:


This is a test(test)test.
This is a (test)test.
This is a(test) test.
This is a ( test) test.
This is (test) test. Do nothing with this and the remaining examples.
This is a (test).
This is a (test)!
This is a (test)?
This is a (test)
This is a (test), test.
This is a "(test)" test.


Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " {2,}\("
.Replacement.Text = " ("
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " {1,}\)"
.Replacement.Text = ")"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\) {2,}"
.Replacement.Text = ") "
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\( {1,}"
.Replacement.Text = "("
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "("
.Replacement.Text = "~~"
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = ")"
.Replacement.Text = "``"
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "([! """ & vbCr & "])~~"
.Replacement.Text = "\1 ("
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "``([! .,\!\?""" & vbCr & "])"
.Replacement.Text = ") \1"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "~~"
.Replacement.Text = "("
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "``"
.Replacement.Text = ")"
.Execute Replace:=wdReplaceAll
End With
End Sub

Snowflake
10-15-2012, 08:54 AM
Wow Greg..Thanks..:)

gmaxey
10-15-2012, 06:52 PM
That can be shortened a bit:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " {2,}\("
.Replacement.Text = " ("
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " {1,}\)"
.Replacement.Text = ")"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\) {2,}"
.Replacement.Text = ") "
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\( {1,}"
.Replacement.Text = "("
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "([! """ & vbCr & "])\("
.Replacement.Text = "\1 ("
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\)([! .,\!\?""" & vbCr & "])"
.Replacement.Text = ") \1"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

End Sub

macropod
10-15-2012, 10:48 PM
Perhaps:
Sub Test()
With ActiveDocument.Content.Find
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = "([! ^09-^13])([(])"
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
.Text = "([)])([0-9A-Za-z])"
.Execute Replace:=wdReplaceAll
.Text = "([ ])[ ]{1,}"
.Replacement.Text = "\1"
.Execute Replace:=wdReplaceAll
.Text = "([(]) "
.Execute Replace:=wdReplaceAll
.Text = " ([)])"
.Execute Replace:=wdReplaceAll
End With
End Sub

Snowflake
10-16-2012, 02:12 PM
Ty Greg and ty macropod as well. Both codes are perfect. :hi:

Paul_Hossler
10-16-2012, 02:32 PM
a super-duper 'do it all at once' way


Paul E. -- I believe that qualifies


Paul H.