Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Moving all words in brackets

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ridders - 04 Jul 2007 08:46 GMT
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.
Klaus Linke - 04 Jul 2007 09:47 GMT
> within Word, I frequently have documents that I review, where text is in
> the
[quoted text clipped - 10 lines]
>
> 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
Ridders - 04 Jul 2007 13:36 GMT
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.

> > within Word, I frequently have documents that I review, where text is in
> > the
[quoted text clipped - 54 lines]
>       Wend
>    Next myRow
Klaus Linke - 04 Jul 2007 15:36 GMT
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
Ridders - 04 Jul 2007 16:26 GMT
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.

> As I said, if you want to delete the original bracket (= "move" the
> bracket), use rngFind.Delete (see code below).
[quoted text clipped - 51 lines]
>       End If
>    Next myRow
Klaus Linke - 04 Jul 2007 16:37 GMT
> 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
Graham Mayor - 04 Jul 2007 09:49 GMT
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.

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> within Word, I frequently have documents that I review, where text is
> in the form of a table.
[quoted text clipped - 7 lines]
>
> Many thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.