Moving all words in brackets

R

Ridders

within Word, I frequently have documents that I review, where text is in the
form of a table.

I review all words that are shown in brackets, but ideally I am trying to
find out how I can highlight all the text in the brackets, and move it to a
column to the right...this way it will be easier for me to review the text in
the brackets.

I hope someone can help!

Many thanks.
 
K

Klaus Linke

Ridders said:
within Word, I frequently have documents that I review, where text is in
the
form of a table.

I review all words that are shown in brackets, but ideally I am trying to
find out how I can highlight all the text in the brackets, and move it to
a
column to the right...this way it will be easier for me to review the text
in
the brackets.

I hope someone can help!

Many thanks.


Hi,

I hope the macro below works.

If you want to delete the original (brackets), replace
rngFind.Collapse (wdCollapseEnd)
with
rngFind.Delete

The macro copies the text to the next column to the right... else you might
change (i_Col + 1) to something else.

Regards,
Klaus


' put the cursor in the column from which you want to copy the brackets,
' then run this macro.
Dim i As Long, i_Col As Long
i_Col = Selection.Columns(1).Cells(1).ColumnIndex
Dim rngFind As Range
Dim rngMove As Range
Dim myRow As ROW
For Each myRow In Selection.Tables(1).Rows
Set rngFind = myRow.Cells(i_Col).Range
Set rngMove = myRow.Cells(i_Col + 1).Range
rngMove.MoveEnd Unit:=wdCharacter, Count:=-1
rngFind.Find.ClearFormatting
rngFind.Find.Replacement.ClearFormatting
With rngFind.Find
.Text = "\(*\)"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
End With
While rngFind.Find.Execute
rngMove.Collapse (wdCollapseEnd)
rngMove.FormattedText = rngFind
rngFind.Collapse (wdCollapseEnd)
rngFind.MoveEndUntil CSet:=Chr(7), Count:=wdForward
rngFind.MoveEnd Unit:=wdCharacter, Count:=-1
Wend
Next myRow
 
G

Graham Mayor

If you can accept all the bracketed text all in one cell, then
CTRL+F (Find)
Search for
\(*\)
with the wildcard and highlight all items found options set.
Run the search. Dismiss the search tool then copy and paste. Each entry will
be on a separate line but in the same cell.
If you want the result in separate cells, you will need to search for each
item and then decide what yo do with it, for which you will need a macro.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Ridders

Hi Klaus,

Thank you for your macro...it's much appreciated.

I can see that the text is successfully being moved to the right hand column
but it is doing 2 things that I don't want it to do :-

1) It is COPYING the text as opposed to MOVING it (the original text needs
to be moved, not replicated)

2) It is pasting in 2 entries of the text...for example, if asking to move
(this is a test) to the right hand column, after running the macro, in the
right hand column you get (this is a test) (this is a test).

It would be greatly appreciated if you could provide me with some more help
regarding this.

Many thanks.
 
K

Klaus Linke

As I said, if you want to delete the original bracket (= "move" the
bracket), use rngFind.Delete (see code below).

There was a bug in the macro that hit when the cell text ended with a
bracket, and with empty cells.
That caused the double brackets you got.

It's a rather typical problem I've run into multiple times:
If the Range you search is empty, you somehow expect to get no matches with
".Wrap = wdFindStop". But instead Word goes off and searches the rest of the
document.

I've hopefully fixed it in the code below.

Regards,
Klaus


Dim i As Long, i_Col As Long
i_Col = Selection.Columns(1).Cells(1).ColumnIndex
Dim rngFind As Range
Dim rngCell As Range
Dim rngMove As Range
Dim myRow As ROW
For Each myRow In Selection.Tables(1).Rows
Set rngCell = myRow.Cells(i_Col).Range
rngCell.MoveEnd Unit:=wdCharacter, Count:=-1
If rngCell.End > rngCell.Start Then
Set rngFind = rngCell.Duplicate
Set rngMove = myRow.Cells(i_Col + 1).Range
rngMove.MoveEnd Unit:=wdCharacter, Count:=-1
rngFind.Find.ClearFormatting
rngFind.Find.Replacement.ClearFormatting
With rngFind.Find
.Text = "\(*\)"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
End With
Do While rngFind.Find.Execute And _
(rngFind.End > rngFind.Start)
rngMove.Collapse (wdCollapseEnd)
rngMove.FormattedText = rngFind
' If you want to keep the original brackets, replace
' the next line with
' rngFind.Collapse(wdCollapseEnd)
rngFind.Delete
If rngFind.End = rngCell.End Then
Exit Do
Else
rngFind.End = rngCell.End
End If
Loop
End If
Next myRow
 
R

Ridders

Hi Klaus,

Thank you kindly for the work you've put into this macro...it now works
perfectly and will be a great help to me.

Many thanks and regards.
 
K

Klaus Linke

Ridders said:
Hi Klaus,

Thank you kindly for the work you've put into this macro...it now works
perfectly and will be a great help to me.

Many thanks and regards.


Glad it works now! The macro is a bit complicated... But don't let that
intimidate you.
Keep trying to automate such repetitive tasks, and come back for help if you
can't get it working.

Regards,
Klaus
 

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