Moving text

A

Amy

Hi, I would like to create a macro in Word where I can:

Select several paragraphs (doesn't matter how many).
Execute the macro.
The macro finds a piece of text (using wildcards) in the first paragraph of
the selection and moves it to the beginning of that paragraph.
Macro then moves on to next paragraph in the selection, finds the text, and
moves it to the beginning of that paragraph.
Etc.

I thought I could do it with Find/Replace, but it doesn't quite seem to work:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(<*>)^9([0-9]{1,})^13"
.Replacement.Text = "_\2^t\1^t1^p"
.Forward = False
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

Any advice would be appreciated.

Thanks!
Amy

But I only want the selection to be affected, nothing else in the document.
 
H

Helmut Weber

Hi Amy,

if you want to process only the paragraphs in the selection,
have a look at this sample:

Sub Macro12()
Dim rSlc As Range ' selection range
Dim rTmp As Range ' temporary range
Dim oPrg As Paragraph
Set rSlc = selection.Range
For Each oPrg In rSlc.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
' your search pattern
End With
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Amy

Thank you. I will give it a try.

Is there a macro that can find a particular string of text in a paragraph,
then move that text to the beginning of the paragraph? Then do the same
action for each paragraph in a selection?

Thank you for any help,
Amy
 
J

Jezebel

No need for a macro -- use Find and Replace. Start with the cursor at the
first relevant paragraph and simply stop when you've done all the paragraphs
you're interested in.
 
H

Helmut Weber

Hi Amy,

Sub Macro12()
Dim rSlc As Range ' selection range
Dim rTmp As Range ' temporary range
Dim oPrg As Paragraph
Set rSlc = selection.Range
For Each oPrg In rSlc.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
.Text = "fox"
If .Execute Then
oPrg.Range.InsertBefore .Text
rTmp.Delete
End If
End With
Next
End Sub

Which doesn't mean it couldn't be done
the way Jezebel suggested.

I just like to do things sometimes
this way and sometimes that way.

Though, at the moment, I can't
think of a way using search and replace,
if the selection would encompass the
first paragraph in a doc.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Amy

Helmut, this macro is excellent. Is it possible to do it with wildcards (see
below)

Sub Macro13()

Dim rSlc As Range ' selection range
Dim rTmp As Range ' temporary range
Dim oPrg As Paragraph
Set rSlc = Selection.Range
For Each oPrg In rSlc.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
.Text = "_([0-9]{1,})^9"
.Forward = False
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
If .Execute Then
oPrg.Range.InsertBefore ???
rTmp.Delete
End If
End With
Next
End Sub
 
H

Helmut Weber

Hi Amy,
Is it possible to do it with wildcards?

Sure, but what are you searching for?

I am assuming for an underscore,
followed by 1 or more digits in the range from 0 til 9,
followed by a tab.
.Text = "_([0-9]{1,})^9" ' no but doesn't do any harm
is the same as
..Text = "_[0-9]{1,}^9"

Without a replacement text,
the parentheses are meaningless.

If you would search for an underscore,
followed by a left parenthesis and 1 or more digits
in the range from 0 til 9, followed by a right parenthesis
followed by a tab,
then the pattern would be:
.Text = "_\([0-9]{1,}\)^9"

Sub Macro12x()
Dim rSlc As Range ' selection range
Dim rTmp As Range ' temporary range
Dim oPrg As Paragraph
Set rSlc = selection.Range
For Each oPrg In rSlc.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
.Text = "_\([0-9]{1,}\)^9"
' or
' .Text = "_[0-9]{1,}^9"
.MatchWildcards = True
If .Execute Then
oPrg.Range.InsertBefore rTmp.Text ' <<<<
rTmp.Delete
End If
End With
Next
End Sub

You might have to adjust the tab in addition to it all.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Amy

Helmut, this works great, thank you.

Now that I've moved "_[0-9]{1,}^9" to the beginning of the paragraph, is
there a way to move it back to the end of the paragraph, before the paragraph
mark?

Thank you. I hope this helps others as well.
Amy
 
H

Helmut Weber

Hi Amy,

like this:

Sub Macro12y()
Dim rSlc As Range ' selection range
Dim rTmp As Range ' temporary range
Dim oPrg As Paragraph
Set rSlc = Selection.Range
For Each oPrg In rSlc.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
.Text = "_[0-9]{1,}^9"
.MatchWildcards = True
If .Execute Then
' oPrg.Range.InsertBefore rTmp.Text ' <<<<
oPrg.Range.Characters.Last.InsertBefore rTmp.Text ' <<<<
rTmp.Delete
End If
End With
Next
End Sub
 

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