Cut and past emails ...

2

2007-User

Hi,

I have a word file containing thousand of email addresses I need to cut all
email addresses included a word "arch" or "architect" and then paste them to
another word file, the email addresses are in separate line, is this
possible by writing a VBA? can anyone please send me sample VBA to do such
thing?

Thanks in advance for your future comments.
 
G

Graham Mayor

The following macro will paste all paragraphs containing "arch" to a
pre-existing document called
D:\My Documents\Test\Versions\newlist.doc
Create and save a blank document and change the name and path in the macro
to match that document's name. It doesn't matter whether the format is doc
or docx (unless you also want to run the macro on Word 2003 without the
compatibility pack.)

Sub ExtractArch()
Dim sAddr As String
Dim iCount As Integer
Dim Count As Long
iCount = ActiveDocument.Paragraphs.Count
Count = 1
Selection.HomeKey Unit:=wdStory
Do Until Count = iCount
ActiveDocument.Bookmarks("\para").Select
If InStr(Selection, "arch") >= 1 Then
Selection.Copy
Documents.Open """D:\My Documents\Test\Versions\newlist.doc"""
Selection.EndKey Unit:=wdStory
Selection.Paste
ActiveDocument.Save
ActiveDocument.Close SaveChanges:=wdSaveChanges
End If
Count = Count + 1
Selection.MoveDown wdLine
Loop
End Sub

http://www.gmayor.com/installing_macro.htm
 
2

2007-User

Thank you so much Graham


Graham Mayor said:
The following macro will paste all paragraphs containing "arch" to a
pre-existing document called
D:\My Documents\Test\Versions\newlist.doc
Create and save a blank document and change the name and path in the macro
to match that document's name. It doesn't matter whether the format is doc
or docx (unless you also want to run the macro on Word 2003 without the
compatibility pack.)

Sub ExtractArch()
Dim sAddr As String
Dim iCount As Integer
Dim Count As Long
iCount = ActiveDocument.Paragraphs.Count
Count = 1
Selection.HomeKey Unit:=wdStory
Do Until Count = iCount
ActiveDocument.Bookmarks("\para").Select
If InStr(Selection, "arch") >= 1 Then
Selection.Copy
Documents.Open """D:\My Documents\Test\Versions\newlist.doc"""
Selection.EndKey Unit:=wdStory
Selection.Paste
ActiveDocument.Save
ActiveDocument.Close SaveChanges:=wdSaveChanges
End If
Count = Count + 1
Selection.MoveDown wdLine
Loop
End Sub

http://www.gmayor.com/installing_macro.htm
 
2

2007-User

Graham,

first of all it works very good "thanks" but I have two questions as
follows:

1- Can search for two words (like "arch" and "structure")?
2- To save time and also get the result a little faster, is it possible to
store the results in memory and then insert them to the destination file at
the end?

Thanks again.
 
G

Graham Mayor

It's the looping that takes most of the time, however the following will
keep the list in memory and write it to a new document on completion

Sub ExtractArch()
Dim sAddr As String
Dim iCount As Integer
Dim Count As Long
iCount = ActiveDocument.Paragraphs.Count
Count = 1
Selection.HomeKey Unit:=wdStory
aAddr = ""
Do Until Count = iCount
ActiveDocument.Bookmarks("\para").Select
If InStr(Selection, "arch") >= 1 _
Or InStr(Selection, "structure") >= 1 Then
sAddr = sAddr & Selection.Range
End If
Count = Count + 1
Selection.MoveDown wdLine
Loop
Documents.Add
Selection.TypeText sAddr
End Sub

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
2

2007-User

2

2007-User

By the way, here is my email address just in case you need it:
(e-mail address removed)
 
G

Graham Mayor

The questions relate to Outlook - I don't know enough about Outlook vba to
comment - checkout the Outlook forum - http://www.gmayor.com/MSNews.htm

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
2

2007-User

This works very good, except when I have a big files (with 1000 of pages)
that gives me overflow error, do you have any idea how I can fix this? is it
possible to transfer the data part by part? or I should specify a bigger
memory to do that?

Thank you.
 
G

Graham Mayor

This is why I suggested the original macro.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
2

2007-User

I see, thanks.


Graham Mayor said:
This is why I suggested the original macro.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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