
Signature
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
Doug is on the right lines here, but the search routine may not give the
results required. Try the following, which extracts all the mergefields from
the document and writes them to another document, then deletes any
duplicates: You end up with a list of all the mergefields in the document.
Sub CopyMergefieldsToOtherDoc()
Dim Source As Document, Target As Document, myRange As Range, sView As
String
Set Source = ActiveDocument
sView = ActiveWindow.View.ShowFieldCodes
Set Target = Documents.Add
Application.ScreenUpdating = False
Source.Activate
ActiveWindow.View.ShowFieldCodes = True
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="^d Merge", _
MatchWildcards:=False, 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
Target.Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Mergefield"
.Replacement.Text = "MERGEFIELD"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Sort FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(*^13)@"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

Signature
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Macro to extract all of the email addresses from a document
>
[quoted text clipped - 42 lines]
>> field name in a data source file but not in a document file.
>> Thanks
mm - 17 Feb 2006 05:43 GMT
Thanks Doug and Graham.
However my point of reference is that I am in my own application, outside of
Word, using COM automation and 'WordBasic' functions to communicate with
Word.
Therefore I don't think I can run the code below from my application to get
the merge field names.
When I use bookmarks instead of merge fields I can invoke the procedure
BookmarkName$(Count) to get the names of bookmarks. I can then 'goto' the
bookmark and then insert text, all through the WordBasic interface.
So my requirement is - to open a word template, ask it for its fields, and
then populate just those fields.
I was hoping I could somehow use mail merge and build the merge data file
with 1 row in it on the fly. I was hoping I could build the merge data file
based on which fields the document needs. However if I can't get the names
of just the fields used in a specific template then I will have to revert
back to using bookmarks.
How about this idea. A merge field can also have a bookmark. If I gave the
merge fields a bookmark name with the same name then I would be all set. I
could ask for the bookmark names and then build a merge data file and
perform a mail merge. What do you think ? Any other ideas ?
Thanks.
Michael.
> Doug is on the right lines here, but the search routine may not give the
> results required. Try the following, which extracts all the mergefields
[quoted text clipped - 105 lines]
>>> field name in a data source file but not in a document file.
>>> Thanks