Cutting and pasting

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
 
R

Rick

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>
 
L

Larry

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
 

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