Delete all text except 7 digit numbers

G

Guest

Hi,

I receive emails that contain item numbers.

All item number are 7 digits long.

Examples:

2734571
#2734845
MLS#2745931

Often the numbers are found within a paragraph/sentence like...

"I want more information about MLS#2745931, #2734845 and 2734571. Could you
tell me more about these things."

What I want to create is some sort of ... macro? Find & Replace operation?
that could extract all the 7 digit numbers from text and simply list them on
a seperate line after being extracted.

Using the quoted text above... I want to end up with a document that just
has the following text remaining...

2745931
2734845
2734571

Can someone tell me how this can be achieved?


Thanks.
 
G

Graham Mayor

Something like

Sub CopyNumbersToNewDoc()
Dim Source As Document
Dim Target As Document
Dim myRange As Range
Dim sView As String

Set Source = ActiveDocument
sView = ActiveWindow.View.ShowFieldCodes
Set Target = Documents.Add
Application.ScreenUpdating = False
Source.Activate
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
Do While .Execute(findText:="[0-9]{7}", _
MatchWildcards:=True, Wrap:=wdFindStop, _
Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = sView
Source.Close SaveChanges:=wdDoNotSaveChanges
Target.Activate
End Sub

should do the trick. http://www.gmayor.com/installing_macro.htm

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

My web site www.gmayor.com

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

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