Find addresses

S

SavitaG

I have multiple word documents, each containing more than one postal
addresses. I want to read through these documents and store the postal
addresses in a separate document file, somehow to mimick what word does upon
selection of
Tools, Letters and Mailings, Envelopes and Labels, on the Labels tab it
shows the address by default.
Any help would great, struggling with it and not able to find any answers
 
D

Doug Robbins - Word MVP

Is there a consistent format to the address that may be used in code to
locate part of it? For example a two upper case letter followed by a Zip
code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

SavitaG

Yes, the address should have a state zip code in the last line.

Due to being new to this i dont know how to pick up the lines above it, as
the lines above could be 2 or 3 or 4 sometimes

Really appreciate your help with this
 
D

Doug Robbins - Word MVP

OK, but what would separate the beginning of the address from any preceding
text? We would need to come up with some way of determining if the
preceding 2, 3 or 4 lines of text should be treated as being part of the
address.

Is each line of the address a separate paragraph (that is, if you click on
the Show.Hide button (¶), what appears at the end of each line?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

SavitaG

Either there would be a blank line which can be tracked by vbCr or
there will be word CC1, CC2, CC3 .....

for example

Martha Stewart
72 Mount Auburn Street
Marlborough, MA 02472

Some text lines will be here
Some text lines will be here
Some text lines will be here

Some text lines will be here
Some text lines will be here

CC1
Gregory Shlimovich
73 West Main Street
Waltham, MA 01748

CC2:
Savita Sharma
74 Main Street
Framingham, MA 02053

Like in the example above there are three addresses
 
D

Doug Robbins - Word MVP

Can you answer this questions:

Is each line of the address a separate paragraph (that is, if you click on
the Show.Hide button (¶), what appears at the end of each line?

And, typically, is there any text before the Martha Stewart in your example?
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

The following should work for a given folder selected from the macro

Sub ExtractAddresses()
Dim SourceDoc As Document
Dim TargetDoc As Document
Dim oRng As Range
Dim strFile As String
Dim strPath As String
Dim iFld As Integer
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select Folder containing the documents and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
Application.ScreenUpdating = False
strFile = Dir$(strPath & "*.do?")
Set TargetDoc = Documents.Add
While strFile <> ""
Set SourceDoc = Documents.Open(strPath & strFile)
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[A-Z]{2} [0-9]{5}"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
While .Execute = True
Set oRng = Selection.Range
oRng.MoveStart wdParagraph, -3
TargetDoc.Range.InsertAfter oRng & vbCr & vbCr
Wend
End With
End With
SourceDoc.Close SaveChanges:=wdDoNotSaveChanges
strFile = Dir$()
Wend
Application.ScreenUpdating = True
TargetDoc.Activate
End Sub

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

P.S. Although it shouldn't affect the original documents, it is best to run
it on a folder containing copies of the documents you want to search - to
avoid complications from the factors that you haven't told us about ;)

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


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


Graham said:
The following should work for a given folder selected from the macro

Sub ExtractAddresses()
Dim SourceDoc As Document
Dim TargetDoc As Document
Dim oRng As Range
Dim strFile As String
Dim strPath As String
Dim iFld As Integer
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select Folder containing the documents and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
Application.ScreenUpdating = False
strFile = Dir$(strPath & "*.do?")
Set TargetDoc = Documents.Add
While strFile <> ""
Set SourceDoc = Documents.Open(strPath & strFile)
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[A-Z]{2} [0-9]{5}"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
While .Execute = True
Set oRng = Selection.Range
oRng.MoveStart wdParagraph, -3
TargetDoc.Range.InsertAfter oRng & vbCr & vbCr
Wend
End With
End With
SourceDoc.Close SaveChanges:=wdDoNotSaveChanges
strFile = Dir$()
Wend
Application.ScreenUpdating = True
TargetDoc.Activate
End Sub

Either there would be a blank line which can be tracked by vbCr or
there will be word CC1, CC2, CC3 .....

for example

Martha Stewart
72 Mount Auburn Street
Marlborough, MA 02472

Some text lines will be here
Some text lines will be here
Some text lines will be here

Some text lines will be here
Some text lines will be here

CC1
Gregory Shlimovich
73 West Main Street
Waltham, MA 01748

CC2:
Savita Sharma
74 Main Street
Framingham, MA 02053

Like in the example above there are three addresses
 

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