View Single Post
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP
 
Posts: n/a
Default How to get all mail merge fields in a document

Macro to extract all of the email addresses from a document

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

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

wrote in message
oups.com...
I am using COM automation and WordBasic to drive Word from another
application.
I have Word templates defined with mail merge fields in them.
I am trying to open one of my Word templates and ask it for all the
mail merge fields in it.
I am doing this because I want to build the data source file on the
fly.
I want to create a single record in the data source and have in it only
the fields required for the current template.
My problem is that I cant find a function which will return me all the
mail merge field names in a document.
I previously tried using bookmarks instead of mail merge fields and
this worked fine. I could ask the document for all its bookmarks and
then get the data transferred across from my app to Word for just the
bookmarks defined in the document.
Does anyone know of a function which will give me the names of all mail
merge fields in a document ?
The function MergeFieldName$(1) will get me the first mail merge field
name in a data source file but not in a document file.
Thanks