macro to remove item between parenthesis

E

Elfego Baca

I have written a 957 page book. During the creation of the book I placed many
phrases between parenthesis to help me remember characters and dates. For
example I have hundreds that are similar to the following: (Amy Justice 749
-memo). I would like a macro that will allow me to delete all occurrences of
(xxxxx -memo). In other words it will remove anything between parenthesis,
including the parenthesis when the end of the phrase is -memo). This would
be enormoulsy helpful. Otherwise I have to go through the entire book page by
page searching for "-memo) and then highlighting the entire phrase and
deleting it one at a time. Ahy help will be appreciated.
 
G

Greg Maxey

Sub FindAndDeleteSpecial()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "\(*\)"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
If Mid(oRng, Len(oRng) - 5, 5) = "-memo" Then oRng.Delete
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub
 
E

Elfego Baca

The line: If Mid(oRng, Len(oRng) - 5, 5) = "-memo" Then oRng.Delete comes
back as an error.

Am I doing something wrong or is the code a problem.

I copied and pasted the code into a new macro and then tried running the macro
 
G

Graham Mayor

Greg's macro works for me, but you could make it even simpler


Sub FindAndDeleteSpecial()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\(*-memo\)"
Do While .Execute(Forward:=True, _
MatchWildcards:=True) = True
oRng.Delete
Loop
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Elfego Baca

--
Butch Cassidy


Graham Mayor said:
Greg's macro works for me, but you could make it even simpler


Sub FindAndDeleteSpecial()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\(*-memo\)"
Do While .Execute(Forward:=True, _
MatchWildcards:=True) = True
oRng.Delete
Loop
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>






.
I have tried both macros. I am having the same problem with both. The macros look for the first open parenthesis and then highlight everything up to and including the -memo) and then delete it. For example suppose I have a paragraph that goes something like this: asdfasdf asd asdf asdf here it is ( asdf asd) asdf wet dfg d hdfg (123, asdf) 2etwer wertwt wert we twer (asasd -memo) asdf asdf asdfasdfasd.
The macros are finding the first open paragraph after the words "here it is
and deleting :\"( asdf asd) asdf wet dfg d hdfg (123, asdf) 2etwer wertwt
wert we twer (asasd -memo)." It should only delete (asasd -memo) and then
continue looking for the next occurrence of the -memo). My thought is that
the macro should actually look for "-memo)" and then somehow highlight
backwards starting from the ending parenthesis till the first occurrence of
the opening parenthesis and then delete the entire section. Right now, as
the subroutines are working, the subroutine looks for the first occurrence of
the opening parenthesis and then highlights everything until it finds the
words in the text "memo" and the closing parenthesis and then deletes the
whole thing. When I tried the subroutines on my 1000 page book it left me
with a 40 page book and all of the rest was deleted.
 
G

Graham Mayor

..
The macros are finding the first open paragraph after the words "here it
is
and deleting :\"( asdf asd) asdf wet dfg d hdfg (123, asdf) 2etwer wertwt
wert we twer (asasd -memo)." It should only delete (asasd -memo) and then
continue looking for the next occurrence of the -memo). My thought is
that
the macro should actually look for "-memo)" and then somehow highlight
backwards starting from the ending parenthesis till the first occurrence
of
the opening parenthesis and then delete the entire section. Right now, as
the subroutines are working, the subroutine looks for the first occurrence
of
the opening parenthesis and then highlights everything until it finds the
words in the text "memo" and the closing parenthesis and then deletes the
whole thing. When I tried the subroutines on my 1000 page book it left me
with a 40 page book and all of the rest was deleted.

The macro looks for a sequence that matches "\(*-memo\)"
i.e. an opening bracket followed by any amount of text until it sees -memo
immediately followed by a closing bracket. If you have bracketed text within
bracketed strings or if you have missing brackets, then inevitably it will
find longer strings than you intend.
You could narrow the search by eliminating strings that contain brackets
e.g.

Sub FindAndDeleteSpecial()
Dim oRng As Range
Dim sText As String
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\(*-memo\)"
Do While .Execute(Forward:=True, _
MatchWildcards:=True) = True
sText = Mid(oRng.Text, 2, Len(oRng.Text) - 2)
If InStr(1, sText, "(") = False Then
If InStr(1, sText, ")") = False Then
MsgBox oRng
'orng.Delete
End If
End If
Loop
End With
End Sub

which finds only (asasd -memo) in your sample text. How accurate this will
be in finding strings in your document is hard to say, but for the purpose
of testing I have used a message box to display the found string and
disabled the delete function. You can disable the message box and enable the
delete function if it works for you.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Elfego Baca

--
Butch Cassidy


Graham Mayor said:
..

The macro looks for a sequence that matches "\(*-memo\)"
i.e. an opening bracket followed by any amount of text until it sees -memo
immediately followed by a closing bracket. If you have bracketed text within
bracketed strings or if you have missing brackets, then inevitably it will
find longer strings than you intend.
You could narrow the search by eliminating strings that contain brackets
e.g.

Sub FindAndDeleteSpecial()
Dim oRng As Range
Dim sText As String
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\(*-memo\)"
Do While .Execute(Forward:=True, _
MatchWildcards:=True) = True
sText = Mid(oRng.Text, 2, Len(oRng.Text) - 2)
If InStr(1, sText, "(") = False Then
If InStr(1, sText, ")") = False Then
MsgBox oRng
'orng.Delete
End If
End If
Loop
End With
End Sub

which finds only (asasd -memo) in your sample text. How accurate this will
be in finding strings in your document is hard to say, but for the purpose
of testing I have used a message box to display the found string and
disabled the delete function. You can disable the message box and enable the
delete function if it works for you.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


.
Can a macro be written that simply looks for the word meme + the end parenthesis 'memo)' and then highlights backwards to a starting parenthesis, then deleting this entire highlighted text? The reason is the macro that you gave me allows me to individually delete the phrases 1 at a time, however there are 6943 instances of the word memo within an opening and closing parenthesis and I can't delete each one, one at a time for 6943 key presses.
 
G

Graham Mayor

You didn't read my earlier reply!

I said that I had inserted the message box for testing to ensure that it was
picking only the strings you requested. If you are satisfied that works then
remove the line
MsgBox oRng
and remove the apostrophe from the start of the line
'orng.Delete

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

As I believe I said earlier it is difficult to allow for input errors. While
missing or duplicated brackets can still cause a problem, on further
reflection the following may be more what you had in mind. This finds the
string
"\(*-memo\)" then searching from the end of the string seeks backwards to
the last opening bracket in the string and moves the start of the range to
that position before deleting the range. As an additional check, the deleted
entries and the page numbers on which they are found are listed in a new
document. You can thus check whether the macro works correctly with your
document.


Sub FindAndDeleteSpecial()
Dim oRng As Range
Dim sText As String
Dim iStart As Integer
Dim iEnd As Integer
Dim oDoc As Document
Dim oNewDoc As Document
Set oDoc = ActiveDocument
Set oNewDoc = Documents.Add
Set oRng = oDoc.Range
With oRng.Find
.Text = "\(*-memo\)"
Do While .Execute(Forward:=True, _
MatchWildcards:=True) = True
iStart = InStrRev(oRng.Text, "(")
oRng.Start = oRng.Start + (iStart - 1)
oNewDoc.Range.InsertAfter "Page " & _
oRng.Information(wdActiveEndPageNumber) & _
vbTab & oRng.Text & vbCr
oRng.Delete
Loop
End With
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top