Cutting and pasting

  • Thread starter Thread starter donh4
  • Start date Start date
D

donh4

I would like to build a VB macro to search thru one document, find
keywords, copy that line to another document.
I know how to do that in Excel but can't seem to get started in Word.
Can someone point me in the right direction.
If I use FIND I can find the word but how do I highlight the entire
line and then copy it.
Thanks in advance for any help.

Don
 
You should read up on the Selection property.

You could do something like this:

<code>

' Find what you're looking for
Selection.Find.Execute

' Once there, go to the beginning of the line
Selection.HomeKey wdLine

' Extend the selection to the end of the line
Selection.Extend
Selection.EndKey wdLine

<code>
 
You could adapt this macro that was provided by Doug Robbins, to your
purpose. This macro copies all the the e-mail addresses in a dcoument
into another document, but you could easily change the code to make it
find what you want.

Basically you need to change the specs inside the quotes in the
following line

Do While .Execute(findText:="[+0-9A-z._-]{1,}\@[A-z.]{1,}", _

to something like this

Do While .Execute(findText:="My text", _

Also, you may need to change the match wild cards argument to false.

Larry



Sub CopyAddressesToOtherDoc()


Dim Source As Document, Target As Document, myRange As Range
Set Source = ActiveDocument
Set Target = Documents.Add

Application.ScreenUpdating = False

Source.Activate
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[+0-9A-z._-]{1,}\@[A-z.]{1,}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With

Selection.HomeKey Unit:=wdStory
Target.Activate

End Sub
 
Back
Top