PDA

View Full Version : [SOLVED:] Include end paragraph mark of previous paragraph in selection



Dave T
03-15-2018, 06:25 AM
Hello All,

Some time ago Greg Maxey helped me out with a find and replace macro that operates within a selected range.
I wanted to insert a tab space after a right parenthesis and found a Cybertext article that uses wildcards to find virtually what I was after:
https://cybertext.wordpress.com/2015/07/14/word-wildcard-find-and-replace-for-numbers-and-trailing-punctuation/

Using most of Greg’s macro I have modified this to search for a number followed by a right parentheses using a wildcard search based on the Cybertext article.

If for example, using the ‘quick brown fox’ text below, I select the first three paragraphs it works fine. However, if I select just the paragraphs with the numbers and right parenthesis it does not inset a tab after the number 1 as the end paragraph mark in the first paragraph was not included in the initial selected range.

The quick brown fox jumps over the lazy dog.
1) The quick brown fox jumps over the lazy dog.
2) The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

What changes do I need to make to the following macro to include the end paragraph mark of the previous paragraph that was not included in the initial selection ???


Sub FindRightParenthesisWithinSelection()
'Code written by Greg Maxey


Dim myRange As Range
Dim oRngBounds As Range

Set myRange = Selection.Range
Set oRngBounds = myRange.Duplicate

With myRange.Find
'Find end paragraph mark then number then right ) followed by a space
.Text = "(^013)([0-9]@)([\)])([ ])"
.wrap = wdFindStop
.MatchWildcards = True
.Forward = True
Do While .Execute
If Not myRange.InRange(oRngBounds) Then Exit Sub
myRange.Select
ActiveWindow.ScrollIntoView Selection.Range
Select Case MsgBox("Do you want to insert a tab after this bracket?", vbQuestion + vbYesNoCancel, "Insert tab after bracket")
Case vbYes
myRange.Characters.Last = vbTab
Case vbNo
'Do nothing.
Case Else
GoTo lbl_Exit
End Select
myRange.Collapse wdCollapseEnd
Loop
End With
lbl_Exit:
Exit Sub
End Sub

Regards,
Dave T

Jfp87
03-15-2018, 07:42 AM
Could you not just change .Text = "(^013)([0-9]@)([\)])([ ])" to .Text = "([0-9]@)([\)])([ ])"

Dave T
03-15-2018, 03:45 PM
Hello Jfp87,

I appreciate your reply.

Yes I did try that, but sometimes the paragraph may contain more than one number followed by a right parentheses and this option affects each case.
For example:

The quick brown fox jumps over the lazy dog.
1) The quick brown fox jumps 2) the low brown fox flops.
3) The quick brown fox jumps over the lazy dog 4) the lazy dog.
The quick brown fox jumps over the lazy dog.

I only want a tab inserted after the first parentheses at the start of each paragraph and not each case after that, hence finding the end paragraph mark of the previous paragraph.

I also had another question as to whether a specific sized hanging indent and tab stop could be incorporated into the macro that was independent of the left indent setting.
For example the left indent could be 1.0cm or 1.5cm or whatever, but the hanging and tab would always be say 1.0cm from whatever the size the left indent may be set to.

Regards,
Dave T

Jfp87
03-16-2018, 03:54 AM
Try this:


Sub FindRightParenthesisWithinSelection()'Code written by Greg Maxey
Dim myRange As Range
Dim oRngBounds As Range


Set myRange = Selection.Range
Set oRngBounds = myRange.Duplicate

With myRange.Find
'Find end paragraph mark then number then right ) followed by a space
.Text = "([0-9]@)([\)])([ ])"
.Wrap = wdFindStop
.MatchWildcards = True
.Forward = True

Do While .Execute

If Not myRange.InRange(oRngBounds) Then Exit Sub

If myRange.Start = myRange.Paragraphs(1).Range.Start Then

myRange.Select
ActiveWindow.ScrollIntoView Selection.Range

Select Case MsgBox("Do you want to insert a tab after this bracket?", vbQuestion + vbYesNoCancel, "Insert tab after bracket")
Case vbYes
myRange.Characters.Last = vbTab
Case vbNo
'Do nothing.
Case Else
GoTo lbl_Exit
End Select

myRange.Collapse wdCollapseEnd

End If

Loop

End With

lbl_Exit:
Exit Sub
End Sub

Dave T
03-19-2018, 03:19 PM
Hello Jfp86,

You solution works perfectly. Thank you for your help.
I had worked out that my selected range needed to go back a couple of characters, but I could not work out how to do it.

Very nice, quick solution.

Regards
Dave T

macropod
03-23-2018, 11:50 PM
sometimes the paragraph may contain more than one number followed by a right parentheses and this option affects each case.
For example:

The quick brown fox jumps over the lazy dog.
1) The quick brown fox jumps 2) the low brown fox flops.
3) The quick brown fox jumps over the lazy dog 4) the lazy dog.
The quick brown fox jumps over the lazy dog.

I only want a tab inserted after the first parentheses at the start of each paragraph and not each case after that, hence finding the end paragraph mark of the previous paragraph.
In that case, all you need is a wildcard Find/Replace with:
Find = ([0-9]{1,}\)) (*^13)
Replace = \1^t\2
and use 'Replace' instead of 'Replace All' to have Word ask whether the insert the tab in a specific instance. No macros required.